Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

Events and Event Filters

In TQt, an event is an object that inherits TQEvent. Events are delivered to objects that inherit TQObject through calling TQObject::event(). Event delivery means that an event has occurred, the TQEvent indicates precisely what, and the TQObject needs to respond. Most events are specific to TQWidget and its subclasses, but there are important events that aren't related to graphics, for example, socket activation, which is the event used by TQSocketNotifier for its work.

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

Most events types have special classes, most commonly TQResizeEvent, TQPaintEvent, TQMouseEvent, TQKeyEvent and TQCloseEvent. There are many others, perhaps forty or so, but most are rather odd.

Each class subclasses TQEvent and adds event-specific functions; see, for example, TQResizeEvent. In the case of TQResizeEvent, TQResizeEvent::size() and TQResizeEvent::oldSize() are added.

Some classes support more than one event type. TQMouseEvent supports mouse moves, presses, shift-presses, drags, clicks, right-presses, etc.

Since programs need to react in varied and complex ways, TQt's event delivery mechanisms are flexible. The documentation for TQApplication::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, TQPaintEvent is delivered by calling TQWidget::paintEvent(). This virtual function is responsible for reacting appropriately, normally by repainting the widget. If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class's implementation; for example:

    MyTable::contentsMouseMoveEvent( TQMouseEvent *me )
    {
        // my implementation

        TQTable::contentsMouseMoveEvent( me ); // hand it on
    }
If you want to replace the base class's function then you must implement everything yourself; but if you only want to extend the base class's functionality, then you implement what you want and then call the base class.

Occasionally there isn't 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 TQWidget to move the keyboard focus, but a few widgets need the tab key for themselves.

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

  bool MyClass:event( TQEvent *evt ) {
      if ( evt->type() == TQEvent::KeyPress ) {
          TQKeyEvent *ke = (TQKeyEvent *)evt;
          if ( ke->key() == Key_Tab ) {
              // special tab handling here
              ke->accept();
              return TRUE;
          }
      } else if ( evt->type() >= TQEvent::User ) {
          TQCustomEvent *ce = (TQCustomEvent*) evt;
          // custom event handling here
          return TRUE;
      }
      return TQWidget::event( evt );
  }

More commonly, an object needs to look at another's events. TQt supports this using TQObject::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 TQObject::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 TQApplication. This is what TQToolTip does in order to see all the mouse and keyboard activity. 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.

Creating an event of a built-in type is very simple: create an object of the relevant type, and then call TQApplication::sendEvent() or TQApplication::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() that 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 TQt'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: TQWidget::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 TQEvent::User, and probably you also need to subclass TQCustomEvent in order to pass characteristics about your custom event. See the documentation to TQCustomEvent for details.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8