1

How force mouseMoveEvent when QPushButton is disabled?

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

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);
}
Minh
  • 1,630
  • 1
  • 8
  • 18
  • It almost works. The mouse position is returned strangely. For example, when I move vertically down, I get values sometimes smaller and sometimes bigger. QPoint(225,343) QPoint(227,335) QPoint(226,356) QPoint(227,348) QPoint(226,369) QPoint(224,361) QPoint(226,374) QPoint(229,355) QPoint(232,368) QPoint(233,352) – Jarosław Pietras Oct 13 '20 at 07:15
  • How are you getting the position? `qDebug() << "mouse move" << static_cast(event)->pos();` works for me. – Minh Oct 13 '20 at 07:19
  • I have exactly the same. Return values are valid for mouseMoveEvent () and event (). – Jarosław Pietras Oct 13 '20 at 07:26
  • Then maybe consider sharing your code. My small example does not have that behavior. – Minh Oct 13 '20 at 07:30
  • When moving my finger, I change the value of the scrollbar and it then returns the position very strangely. – Jarosław Pietras Oct 13 '20 at 07:57
  • I can be because the scroll bar's position is changed as you said. But without the actual code, I cannot help you much. – Minh Oct 13 '20 at 08:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222946/discussion-between-ngoc-minh-nguyen-and-jaroslaw-pietras). – Minh Oct 13 '20 at 08:42