mouseMoveEvent not being called
-
I have subclassed QFrame, set mouse tracking to true, and reimplemented
mouseMoveEvent(...), but the function is only called when the mouse enters the frame, exits the frame, or is clicked inside the frame. I have tried setting the focus policy on the frame toStrongFocus, and setting it toNoFocusfor all its child widgets, but that had no effect. It looks like the event is captured by some other widget and blocked, but I can't figure out where.I am using Qt 5.15, this problem is present in Windows, Linux and macOS
-
Hi,
Please post a minimal compilable example that shows that behaviour.
-
I managed to find a solution... I overrode
eventFilter(...)in my QFrame subclass:bool MouseTrackingFrame::eventFilter(QObject *watched, QEvent *event) { static int c = 0; qDebug() << "EVENT: " << ++c << " - " << event->type(); if (event->type() == QEvent::MouseMove) { mouseMoveEvent(static_cast<QMouseEvent *>(event) ); return false; } return QFrame::eventFilter(watched, event); }Then I recursively installed it to all its child widgets, as well as enabling mouse tracking like this:
void install_filter(QObject *target, QObject *filter) { if (target->isWidgetType() ) { qDebug("INSTALL"); static_cast<QWidget *>(target)->setMouseTracking(true); target->installEventFilter(filter); } const QObjectList &children = target->children(); for (auto i = children.begin(); i < children.end(); i++) { install_filter(*i, filter); } } void MouseTrackingFrame::postSetup() { install_filter(this, this); } ... MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) ... { ui->setupUi(this); ui->mouseTrackingFrame->postSetup(); ... } -
do you have the following line in the constructor of class MouseTrackingFrame?
installEventFilter( this );@JoeCFD said in mouseMoveEvent not being called:
do you have the following line in the constructor of class MouseTrackingFrame?
installEventFilter( this );No, the
install_filter(...)function being called fromMouseTrackingFrame::postSetup()does that. But I'm not sure if it has any effect - I saw a post somewhere which said Qt ignores when the widget itself is passed toinstallEventFilter(...)