QT eventFilter with MouseButtonRelease on QListWidget is not detecting mouse press/release
Asked Answered
I

1

5

It should be simple but somehow it's not working as it should. I'm trying to catch with eventFilter mouse button press or release on QListWidget. ListWidget was prepared under UI. I've installed eventFilter like this

ui->listWidget->installEventFilter(this);

I've added in header under public:

bool eventFilter(QObject *obj, QEvent *event);

And created under MainWindow

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{

if (event->type() == QEvent::KeyPress)
{

    qDebug() << "Keyboard press";

} else if (event->type() == QEvent::MouseButtonRelease) {

    qDebug() << "Mouse press L";

} else if(event->type() == QEvent::ContextMenu) {

    qDebug() << "Mouse press R";
}

return QObject::eventFilter(obj, event);
}

I've checked in the docs and It says that every event is being passed to eventHandler before sending to given QWidget. And it's partially true. Because KeyPress and ContextMenu is working. Even if list widget was set to blocksingals(true).

The problem is that MouseButtonRelease / Press is not working. Something is blocking it and I don't know what or how to make it working. I have on_listWidget_clicked also but even getting rid of it it is still not working.

Please help. Thanks

Iene answered 26/2, 2017 at 21:53 Comment(0)
I
7

Something is blocking it

That's right, when you press/release your mouse button on the QListWidget, The QMouseEvent does not get sent to the QListWidget. Instead, the event is sent to the widget where the mouse event happened, that is QListWidget's viewport.

In fact, All the events mentioned in your question were sent to the viewport (since this is were the event actually happened). But since the viewport ignored them, these events propagated to the viewport's parent widget (the QListWidget) where you installed your event filter and intercepted them.

In order to be able to intercept the click event on a QListWidget, You have to either subclass QListWidget and override viewportEvent() (and handle whatever events you are interested in there), or you can install your eventfilter on the viewport instead:

ui->listWidget->viewport()->installEventFilter(this);

in this second case you could need also to enable mouse tracking with the code:

ui->listWidget->setMouseTracking(true);
Incorporate answered 26/2, 2017 at 22:51 Comment(1)
↑ awesome. this works. (tried in python)Reclamation

© 2022 - 2024 — McMap. All rights reserved.