You are not logged in.

Dear visitor, welcome to If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.


Friday, July 20th 2012, 7:18am

How to install a eventFilter for QApplication to deal with all key event?

My GUI app has a main dialog which contains many widgets. I don't want any of the widget receives key press event, but deal with it in main dialog.

I know that Qt has 5 level of event processing. I think overwriting QApplication's eventFilter makes sence, since all events will be handled in qApp, then dispatched to the right objects. So I create a new class MyApplication inherits QApplication, then re-write the eventFilter() function:

bool QViewerApplication::eventFilter(QObject * watched, QEvent * event)
if (event->type() == QEvent::KeyPress)
// do something
return true;
QApplication::eventFilter(watched, event);

However, nothing happens, Key press event won't reach the eventFilter of MyApplication, even I add a line "qApp->installEventFilter(qApp)" in main().

Could anyone tell me what's the problem? How can I handle all key event in one place?


Thursday, July 26th 2012, 11:27pm

We have an event filter on our application. It is a separate class that inherits from QObject with a constructor and the eventFilter methods. And this class get installed at the event filter.