MouseMoveEvent not trigger when button is disabled

401 Views Asked by At

How force mouseMoveEvent when QPushButton is disabled?

I have disabled QPushButton on QScrollArea and on swipe mouseMoveEvent is not trigged.

1

There are 1 best solutions below

7
On

You can use eventFilter like this:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    bool eventFilter(QObject *obj, QEvent *event) override;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

#include <QMouseEvent>
#include <QHBoxLayout>
#include <QDebug>
#include <QPushButton>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QPushButton *button = new QPushButton{this};
    button->installEventFilter(this);
    button->setMouseTracking(true);
    button->setText("bla");
    button->setDisabled(true);

    QWidget *widget = new QWidget{this};
    QHBoxLayout *layout = new QHBoxLayout{widget};
    layout->setContentsMargins(50, 50, 50, 50);
    layout->addWidget(button);
    this->setCentralWidget(widget);
}

MainWindow::~MainWindow()
{
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseMove)
    {
        qDebug() << "mouse move" << static_cast<QMouseEvent*>(event)->pos();
        return true;
    }
    else
        return QMainWindow::eventFilter(obj, event);
}