~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/eventsandfilters.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/eventsandfilters.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Events and Event Filters</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Events and Event Filters</h1>
 
21
<p>In Qt, an event is an object that inherits <a href="qevent.html">QEvent</a>. Events are delivered to objects that inherit <a href="qobject.html">QObject</a> through calling <a href="qobject.html#event">QObject::event</a>(). Event delivery means that an event has occurred, the <a href="qevent.html">QEvent</a> indicates precisely what, and the <a href="qobject.html">QObject</a> needs to respond. Most events are specific to <a href="qwidget.html">QWidget</a> and its subclasses, but there are important events that aren't related to graphics (e.g., <a href="qtimer.html">timer events</a>).</p>
 
22
<p>Some events come from the window system (e.g., <a href="qmouseevent.html">QMouseEvent</a>), some from other sources (e.g., <a href="qtimerevent.html">QTimerEvent</a>), 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 using <a href="qcoreapplication.html#sendEvent">QCoreApplication::sendEvent</a>() and <a href="qcoreapplication.html#postEvent">QCoreApplication::postEvent</a>().</p>
 
23
<p>Most events types have special classes, notably <a href="qresizeevent.html">QResizeEvent</a>, <a href="qpaintevent.html">QPaintEvent</a>, <a href="qmouseevent.html">QMouseEvent</a>, <a href="qkeyevent.html">QKeyEvent</a>, and <a href="qcloseevent.html">QCloseEvent</a>. Each class subclasses <a href="qevent.html">QEvent</a> and adds event-specific functions; see for example <a href="qresizeevent.html">QResizeEvent</a>, which adds <a href="qresizeevent.html#size">QResizeEvent::size</a>() and <a href="qresizeevent.html#oldSize">QResizeEvent::oldSize</a>().</p>
 
24
<p>Some classes support more than one actual event type. <a href="qmouseevent.html">QMouseEvent</a> supports mouse button presses, double-clicks, moves, etc. The event type is available as <a href="qevent.html#type">QEvent::type</a>().</p>
 
25
<p>Since programs need to react in varied and complex ways, Qt's event delivery mechanisms are flexible. The documentation for <a href="qcoreapplication.html#notify">QCoreApplication::notify</a>() 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</a> rehashes it less concisely; here we will explain enough for 95% of applications.</p>
 
26
<p>The normal way for an event to be delivered is by calling a virtual function. For example, <a href="qpaintevent.html">QPaintEvent</a> is delivered by calling <a href="qwidget.html#paintEvent">QWidget::paintEvent</a>(). 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:</p>
 
27
<pre>&nbsp;   void MyCheckBox::mousePressEvent(QMouseEvent *event)
 
28
    {
 
29
        if (event-&gt;button() == Qt::LeftButton) {
 
30
            // handle left mouse button here
 
31
        } else {
 
32
            // pass on other buttons to base class
 
33
            QCheckBox::mousePressEvent(event);
 
34
        }
 
35
    }</pre>
 
36
<p>If you want to replace the base class's function, 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.</p>
 
37
<p>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 <a href="qwidget.html">QWidget</a> to move the keyboard focus separately from the other keys, but a few widgets need the <b>Tab</b> key for themselves.</p>
 
38
<p>These objects can reimplement <a href="qobject.html#event">QObject::event</a>(), 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 the following <a href="qobject.html#event">event()</a> function:</p>
 
39
<pre>&nbsp;   bool MyWidget::event(QEvent *event)
 
40
    {
 
41
        if (event-&gt;type() == QEvent::KeyPress) {
 
42
            QKeyEvent *ke = static_cast&lt;QKeyEvent *&gt;(event);
 
43
            if (ke-&gt;key() == Qt::Key_Tab) {
 
44
                // special tab handling here
 
45
                return true;
 
46
            }
 
47
        } else if (event-&gt;type() == MyCustomEventType) {
 
48
            MyCustomEvent *myEvent = static_cast&lt;MyCustomEvent *&gt;(event);
 
49
            // custom event handling here
 
50
            return true;
 
51
        }
 
52
 
 
53
        return QWidget::event(event);
 
54
    }</pre>
 
55
<p>More commonly, an object needs to look at another's events. Qt supports this using <a href="qobject.html#installEventFilter">QObject::installEventFilter</a>() (and the corresponding remove). For example, dialogs commonly want to filter key presses for some widgets, e.g. to modify <b>Return</b>-key handling.</p>
 
56
<p>An event filter gets to process events before the target object does. The filter's <a href="qobject.html#eventFilter">QObject::eventFilter</a>() 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.</p>
 
57
<p>It's also possible to filter <i>all</i> events for the entire application, by installing an event filter on the <a href="qapplication.html">QApplication</a> or <a href="qcoreapplication.html">QCoreApplication</a> object. This is very powerful, but it also slows down event delivery of every single event in the entire application, so it's best avoided.</p>
 
58
<p>The global event filters are called before the object-specific filters.</p>
 
59
<p>Finally, many applications want to create and send their own events.</p>
 
60
<p>Creating an event of a built-in type is very simple: create an object of the relevant type, and then call <a href="qcoreapplication.html#sendEvent">QCoreApplication::sendEvent</a>() or <a href="qcoreapplication.html#postEvent">QCoreApplication::postEvent</a>().</p>
 
61
<p>sendEvent() processes the event immediately. When sendEvent() returns, the event filters and/or 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.</p>
 
62
<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 compacted into one. The same applies to paint events: <a href="qwidget.html#update">QWidget::update</a>() calls postEvent(), which eliminates flickering and increases speed by avoiding multiple repaints.</p>
 
63
<p>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.</p>
 
64
<p>To create events of a custom type, you need to define an event number, which must be greater than <tt>QEvent::User</tt>, and probably you also need to subclass <a href="qevent.html">QEvent</a> in order to pass characteristics about your custom event. See the <a href="qevent.html">QEvent</a> documentation for details.</p>
 
65
<p /><address><hr /><div align="center">
 
66
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
67
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
68
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
69
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
70
</tr></table></div></address></body>
 
71
</html>