HomeHome

Events and Event Filters


An event, in Qt, is an object that inherits QEvent. Events are delivered to objects that inherit QObject through calling QObject::event(). Event delivery means that an event has occurred, the QEvent indicates precisely what, and the QObject needs to react. Most events are specific to QWidget and its subclasses, but there are important events that aren't related to graphics, such as socket activation, which is the event used by QSocketNotifier for its work.

Some events come from the window system, e.g. QMouseEvent, some from other sources, e.g. QTimerEvent, and some come from the application program. Qt is symmetric, as usual, so you can send events in exactly the same ways as Qt's own event loop does.

Most events types have special classes, most commonly QResizeEvent, QPaintEvent, QMouseEvent, QKeyEvent and QCloseEvent. There are many others, perhaps forty or so, but most are rather odd.

Each class, QResizeEvent is an excellent example, subclasses QEvent and adds event-specific functions. In the case of QResizeEvent, QResizeEvent::size() and QResizeEvent::oldSize() are added.

Some classes support more than one event type. QMouseEvent supports mouse moves, presses, shift-presses, drags, clicks, right-presses, and so on ad infinitum.

Since programs need to react in varied and complicated ways, Qt's event delivery mechanisms are rather flexible. The documentation for QApplication::notify() concisely tells the whole story, here we will explain enough for 99% of applications.

The normal way for an event to be delivered is by calling a virtual function. For example, QPaintEvent is delivered by calling QWidget::paintEvent(). This virtual function is responsible for reacting appropriately, normally by repainting the widget.

Occasionally there isn't a such an event-specific function, or the event-specific function isn't sufficient. The most common example is tab key presses. Normally, those are interpreted by QWidget to move the keyboard focus, but a few widgets need the tab key for themselves.

These objects can reimplement QObject::event(), the general event handler, and either do their event handling before or after the usual handling, or replace it completely. A very odd widget that both interprets tab and has an application-specific custom event might contain:

  bool MyClass:event( QEvent * e ) {
      if ( e->type() == QEvent::KeyPress ) {
          QKeyEvent * ke = (QKeyEvent*) e;
          if ( ke->key() == Key_Tab ) {
              // special tab handling here
              k->accept();
              return TRUE;
          }
      } else if ( e->type() >= QEvent::User ) {
          QCustomEvent * c = (QCustomEvent*) e;
          // custom event handling here
          return TRUE;
      }
      QWidget::event( e );
  }

More commonly, an object needs to look at another's events. Qt supports this using QObject::installEventFilter() (and the corresponding remove). For example, dialogs commonly want to filter key presses for some widgets, e.g. to modify Return-key handling.

An event filter gets to process events before the target object does. The filter's QObject::eventFilter() implementation is called, and can accept or reject the filter, and allow or deny further processing of the event. If all the event filters allow further processing of an event, the event is sent to the target object itself. If one of them stops processing, the target and any later event filters don't get to see the event at all.

It's also possible to filter all events for the entire application, by installing an event filter on QApplication. This is what QToolTip does in order to see all the mouse and keyboard activity. Of course, this is very powerful, but it also slows down event delivery of every single event in the entire application, so it's best avoided.

The global event filters are called before the object-specific filters.

Finally, many applications want to create and send their own events.

Creatint an event of a built-in type is very simple: Create an object of the relevant type, and then call QApplication::sendEvent() or QApplication::postEvent().

sendEvent() processes the event immediately - when sendEvent() returns, (the event filters and) the object have already processed the event. For many event classes there is a function called isAccepted() tells you whether the event was accepted or rejected by the last handler that was called.

postEvent() posts the event on a queue for later dispatch. The next time Qt's main event loop runs, it dispatches all posted events, with some optimization. For example, if there are several resize events, they are are compacted into one. The same applies to paint events: QWidget::update() calls postEvent(), which minimizes flickering and increases speed by avoiding multiple repaints.

postEvent() is also often used during object initialization, since the posted event will typically be dispatched very soon after the initialization of the object is complete.

To create events of a custom type, you need to define an event number, which must be greater than QEvent::User, and probably you also need to subclass QCustomEvent in order to pass characteristics about your custom event. See the documentation to QCustomEvent for details.


Copyright © 2000 TrolltechTrademarks
Qt version 2.2.1