source: trunk/doc/html/timers.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 6.3 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/object.doc:86 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Timers</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Timers</h1>
33
34
35
36<p> <a href="qobject.html">QObject</a>, the base class of all Qt objects, provides the basic timer
37support in Qt. With <a href="qobject.html#startTimer">QObject::startTimer</a>(), you start a timer with
38an <em>interval</em> in milliseconds as argument. The function returns a
39unique integer timer id. The timer will now "fire" every <em>interval</em>
40milliseconds, until you explicitly call <a href="qobject.html#killTimer">QObject::killTimer</a>() with
41the timer id.
42<p> For this mechanism to work, the application must run in an event
43loop. You start an event loop with <a href="qapplication.html#exec">QApplication::exec</a>(). When a
44timer fires, the application sends a <a href="qtimerevent.html">QTimerEvent</a>, and the flow of
45control leaves the event loop until the timer event is processed. This
46implies that a timer cannot fire while your application is busy doing
47something else. In other words: the accuracy of timers depends on the
48granularity of your application.
49<p> There is practically no upper limit for the interval value (more than
50one year is possible). The accuracy depends on the underlying operating
51system. Windows 95/98 has 55 millisecond (18.2 times per second)
52accuracy; other systems that we have tested (UNIX X11 and Windows NT)
53can handle 1 millisecond intervals.
54<p> The main API for the timer functionality is <a href="qtimer.html">QTimer</a>. That class
55provides regular timers that emit a signal when the timer fires, and
56inherits <a href="qobject.html">QObject</a> so that it fits well into the ownership structure
57of most GUI programs. The normal way of using it is like this:
58<pre>
59 <a href="qtimer.html">QTimer</a> * counter = new <a href="qtimer.html">QTimer</a>( this );
60 connect( counter, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()),
61 this, SLOT(updateCaption()) );
62 counter-&gt;<a href="qtimer.html#start">start</a>( 1000 );
63</pre>
64
65<p> The counter timer is made into a child of this widget, so that when
66this widget is deleted, the timer is deleted too. Next, its timeout
67signal is connected to the slot that will do the work, and finally
68it's started.
69<p> <a href="qtimer.html">QTimer</a> also provides a simple one-shot timer API. <a href="qbutton.html">QButton</a> uses this
70to show the button being pressed down and then (0.1 seconds later) be
71released when the keyboard is used to "press" a button, for example:
72<p> <pre>
73 QTimer::<a href="qtimer.html#singleShot">singleShot</a>( 100, this, SLOT(animateTimeout()) );
74</pre>
75
76<p> 0.1 seconds after this line of code is executed, the same button's
77animateTimeout() slot is called.
78<p> Here is an outline of a slightly longer example that combines object
79communication via signals and slots with a QTimer object. It
80demonstrates how to use timers to perform intensive calculations in a
81single-threaded application without blocking the user interface.
82<p> <pre>
83 // The Mandelbrot class uses a QTimer to calculate the mandelbrot
84 // set one scanline at a time without blocking the CPU. It
85 // inherits QObject to use signals and slots. Calling start()
86 // starts the calculation. The done() signal is emitted when it
87 // has finished. Note that this example is not complete, just an
88 // outline.
89
90 class Mandelbrot : public <a href="qobject.html">QObject</a>
91 {
92 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> // required for signals/slots
93 public:
94 Mandelbrot( <a href="qobject.html">QObject</a> *parent=0, const char *name );
95 ...
96 public slots:
97 void start();
98 signals:
99 void done();
100 private slots:
101 void calculate();
102 private:
103 <a href="qtimer.html">QTimer</a> timer;
104 ...
105 };
106
107 //
108 // Constructs and initializes a Mandelbrot object.
109 //
110
111 Mandelbrot::Mandelbrot( <a href="qobject.html">QObject</a> *parent=0, const char *name )
112 : <a href="qobject.html">QObject</a>( parent, name )
113 {
114 <a href="qobject.html#connect">connect</a>( &amp;timer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), SLOT(calculate()) );
115 ...
116 }
117
118 //
119 // Starts the calculation task. The internal calculate() slot
120 // will be activated every 10 milliseconds.
121 //
122
123 void Mandelbrot::start()
124 {
125 if ( !timer.<a href="qtimer.html#isActive">isActive</a>() ) // not already running
126 timer.<a href="qtimer.html#start">start</a>( 10 ); // timeout every 10 ms
127 }
128
129 //
130 // Calculates one scanline at a time.
131 // Emits the done() signal when finished.
132 //
133
134 void Mandelbrot::calculate()
135 {
136 ... // perform the calculation for a scanline
137 if ( finished ) { // no more scanlines
138 timer.<a href="qtimer.html#stop">stop</a>();
139 emit done();
140 }
141 }
142 </pre>
143
144<p>
145<!-- eof -->
146<p><address><hr><div align=center>
147<table width=100% cellspacing=0 border=0><tr>
148<td>Copyright &copy; 2007
149<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
150<td align=right><div align=right>Qt 3.3.8</div>
151</table></div></address></body>
152</html>
Note: See TracBrowser for help on using the repository browser.