Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 321

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qt-4.3/doc/src/eventsandfilters.qdoc -->
<head>
  <title>Events and Event Filters</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Events and Event Filters<br /><small></small></h1>
<p>In Qt, events are objects, derived from the abstract <a href="core/QEvent.html"><tt>QEvent</tt></a> class, that represent things that have happened either within an application or as a result of outside activity that the application needs to know about. Events can be received and handled by any instance of a <a href="core/QObject.html"><tt>QObject</tt></a> subclass, but they are especially relevant to widgets. This document describes how events are delivered and handled in a typical application.</p>
<ul><li><a href="#how-events-are-delivered">How Events are Delivered</a></li>
<li><a href="#event-types">Event Types</a></li>
<li><a href="#event-handlers">Event Handlers</a></li>
<li><a href="#event-filters">Event Filters</a></li>
<li><a href="#sending-events">Sending Events</a></li>
</ul>
<a name="how-events-are-delivered"></a>
<h3>How Events are Delivered</h3>
<p>When an event occurs, Qt creates an event object to represent it by constructing an instance of the appropriate <a href="core/QEvent.html"><tt>QEvent</tt></a> subclass, and delivers it to a particular instance of <a href="core/QObject.html"><tt>QObject</tt></a> (or one of its subclasses) by calling its event() function.</p>
<p>This function does not handle the event itself; based on the type of event delivered, it calls an event handler for that specific type of event, and sends a response based on whether the event was accepted or ignored.</p>
<p>Some events, such as <a href="gui/QMouseEvent.html"><tt>QMouseEvent</tt></a> and <a href="gui/QKeyEvent.html"><tt>QKeyEvent</tt></a>, come from the window system; some, such as <a href="core/QTimerEvent.html"><tt>QTimerEvent</tt></a>, come from other sources; some come from the application itself.</p>
<a name="event-types"></a>
<h3>Event Types</h3>
<p>Most events types have special classes, notably <a href="gui/QResizeEvent.html"><tt>QResizeEvent</tt></a>, <a href="gui/QPaintEvent.html"><tt>QPaintEvent</tt></a>, <a href="gui/QMouseEvent.html"><tt>QMouseEvent</tt></a>, <a href="gui/QKeyEvent.html"><tt>QKeyEvent</tt></a>, and <a href="gui/QCloseEvent.html"><tt>QCloseEvent</tt></a>. Each class subclasses <a href="core/QEvent.html"><tt>QEvent</tt></a> and adds event-specific functions. For example, <a href="gui/QResizeEvent.html"><tt>QResizeEvent</tt></a> adds size() and oldSize() to enable widgets to discover how their dimensions have been changed.</p>
<p>Some classes support more than one actual event type. <a href="gui/QMouseEvent.html"><tt>QMouseEvent</tt></a> supports mouse button presses, double-clicks, moves, and other related operations.</p>
<p>Each event has an associated type, defined in QEvent::Type, and this can be used as a convenient source of run-time type information to quickly determine which subclass a given event object was constructed from.</p>
<p>Since programs need to react in varied and complex ways, Qt's event delivery mechanisms are flexible. The documentation for QCoreApplication::notify() concisely tells the whole story; the <i>Qt Quarterly</i> article <a href="http://doc.trolltech.com/qq/qq11-events.html">Another Look at Events</tt></a> rehashes it less concisely. Here we will explain enough for 95% of applications.</p>
<a name="event-handlers"></a>
<h3>Event Handlers</h3>
<p>The normal way for an event to be delivered is by calling a virtual function. For example, <a href="gui/QPaintEvent.html"><tt>QPaintEvent</tt></a> is delivered by calling QWidget::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.</p>
<p>For example, the following code handles left mouse button clicks on a custom checkbox widget while passing all other button clicks to the base <a href="gui/QCheckBox.html"><tt>QCheckBox</tt></a> class:</p>
<pre>    void MyCheckBox::mousePressEvent(QMouseEvent *event)
    {
        if (event-&gt;button() == Qt::LeftButton) {
            <span class="comment">// handle left mouse button here</span>
        } else {
            <span class="comment">// pass on other buttons to base class</span>
            QCheckBox::mousePressEvent(event);
        }
    }</pre>
<p>If you want to replace the base class's function, you must implement everything yourself. However, if you only want to extend the base class's functionality, then you implement what you want and call the base class to obtain the default behavior for any cases you do not want to handle.</p>
<p>Occasionally, there isn't such an event-specific function, or the event-specific function isn't sufficient. The most common example involves <b>Tab</b> key presses. Normally, <a href="gui/QWidget.html"><tt>QWidget</tt></a> intercepts these to move the keyboard focus, but a few widgets need the <b>Tab</b> key for themselves.</p>
<p>These objects can reimplement QObject::event(), the general event handler, and either do their event handling before or after the usual handling, or they can replace the function completely. A very unusual widget that both interprets <b>Tab</b> and has an application-specific custom event might contain the following event() function:</p>
<pre>    bool MyWidget::event(QEvent *event)
    {
        if (event-&gt;type() == QEvent::KeyPress) {
            QKeyEvent *ke = static_cast&lt;QKeyEvent *&gt;(event);
            if (ke-&gt;key() == Qt::Key_Tab) {
                <span class="comment">// special tab handling here</span>
                return true;
            }
        } else if (event-&gt;type() == MyCustomEventType) {
            MyCustomEvent *myEvent = static_cast&lt;MyCustomEvent *&gt;(event);
            <span class="comment">// custom event handling here</span>
            return true;
        }

        return QWidget::event(event);
    }</pre>
<p>Note that QWidget::event() is still called for all of the cases not handled, and that the return value indicates whether an event was dealt with; a <tt>true</tt> value prevents the event from being sent on to other objects.</p>
<a name="event-filters"></a>
<h3>Event Filters</h3>
<p>Sometimes an object needs to look at, and possibly intercept, the events that are delivered to another object. For example, dialogs commonly want to filter key presses for some widgets; for example, to modify <b>Return</b>-key handling.</p>
<p>The QObject::installEventFilter() function enables this by setting up an <i>event filter</i>, causing a nominated filter object to receive the events for a target object in its QObject::eventFilter() function. An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required. An existing event filter can be removed using the QObject::removeEventFilter() function.</p>
<p>When the filter object's eventFilter() implementation is called, it can accept or reject the event, and allow or deny further processing of the event. If all the event filters allow further processing of an event (by each returning <tt>false</tt>), the event is sent to the target object itself. If one of them stops processing (by returning <tt>true</tt>), the target and any later event filters do not get to see the event at all.</p>
<pre>    bool FilterObject::eventFilter(QObject *object, QEvent *event)
    {
        if (object == target &amp;&amp; event-&gt;type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast&lt;QKeyEvent *&gt;(event);
            if (keyEvent-&gt;key() == Qt::Key_Tab) {
                <span class="comment">// Special tab handling</span>
                return true;
            } else
                return false;
        }
        return false;
    }</pre>
<p>The above code shows another way to intercept <b>Tab</b> key press events sent to a particular target widget. In this case, the filter handles the relevant events and returns <tt>true</tt> to stop them from being processed any further. All other events are ignored, and the filter returns <tt>false</tt> to allow them to be sent on to the target widget, via any other event filters that are installed on it.</p>
<p>It is also possible to filter <i>all</i> events for the entire application, by installing an event filter on the <a href="gui/QApplication.html"><tt>QApplication</tt></a> or <a href="core/QCoreApplication.html"><tt>QCoreApplication</tt></a> object. Such global event filters are called before the object-specific filters. This is very powerful, but it also slows down event delivery of every single event in the entire application; the other techniques discussed should generally be used instead.</p>
<a name="sending-events"></a>
<h3>Sending Events</h3>
<p>Many applications want to create and send their own events. You can send events in exactly the same ways as Qt's own event loop by constructing suitable event objects and sending them with QCoreApplication::sendEvent() and QCoreApplication::postEvent().</p>
<p>sendEvent() processes the event immediately. When it returns, the event filters and/or the object itself 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.</p>
<p>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 compressed into one. The same applies to paint events: QWidget::update() calls postEvent(), which eliminates flickering and increases speed by avoiding multiple repaints.</p>
<p>postEvent() is also used during object initialization, since the posted event will typically be dispatched very soon after the initialization of the object is complete. When implementing a widget, it is important to realise that events can be delivered very early in its lifetime so, in its constructor, be sure to initialize member variables early on, before there's any chance that it might receive an event.</p>
<p>To create events of a custom type, you need to define an event number, which must be greater than QEvent::User, and you may need to subclass <a href="core/QEvent.html"><tt>QEvent</tt></a> in order to pass specific information about your custom event. See the <a href="core/QEvent.html"><tt>QEvent</tt></a> documentation for further details.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>