Qt logo


Events and Event Filters


Events in Qt inherit QEvent. They are either sent to an object with QApplication::sendEvent() or posted with QApplication::postEvent(). Sent events are delivered immediately, i.e. when the call to sendEvent() returns, the event handling was completely. Posted events are delivered when the flow of control enters or leaves the main event loop. Posting events has two main advantages:

A QObject receives an event in its event() handler. The general event() implementation distributes certain events to special handlers, such as childEvent() for QObject or paintEvent() for QWidget.

Every QObject can also serve as an event filter for one ore more other objects. By calling installEventFilter(), you declare another object a event filter for this object. The event filter will receive all events sent to the object in advance via its eventFilter() function. In addition to just seeing the events, the filter actually can filter out certain events.

All events are sent through the unique application object, which is an instance of QApplication. Since QApplication is also a QObject, you can just as well install an event filter on it. This so-called global event filter will receive all events, before even the other event filters see them.

A last - discouraged - way of filtering events is to subclass QApplication and to reimplement the notify() function.

Sending and receiving events as communication mechanism in Qt is not limited to the built-in event types. You can easily create custom events and send them around. See the class documentation to QCustomEvent for details.


Copyright © 2000 Troll TechTrademarks
Qt version 2.1.0-beta1