PDA

View Full Version : Eventfilter delay



tvj4218
8th July 2017, 04:29
I've got an app with multiple tabs. I install an event filter on qApp to handle keystrokes as I would like to process certain keystrokes as shortcuts to do certain things.

For the most part it works, except when it doesn't.

Sometimes the keystrokes just don't get processed. After a while I noticed that when the keystrokes did not work, if I clicked around on various tabs, then after a couple of clicks of the mouse the keystrokes just start working.

What's going on and how do I fix it?

n

d_stranz
8th July 2017, 16:31
If you're installing an event filter on the main application instance, then you should be seeing all the events from the main thread all the time. Have you read the docs on events and filters (https://doc.qt.io/qt-5/eventsandfilters.html), especially the parts about returning true or false from your event filter?

I for one would probably never install an event filter on the application instance. There is too much danger of unintended consequences - things stop working or act strangely because there is some circumstance you haven't checked for in your handler and thus the wrong action occurs. I would probably install an event filter on an object instance closer to where the action takes place, like on the QTabWidget or the QMainWindow.

There might be other ways to implement this same functionality without using event filters, such as by installing a QAction with the keystroke shortcut on the tab or main window widget. See QWidget::addAction() and Qt::ShortcutContext.