summaryrefslogtreecommitdiffstats
path: root/doc/html/timers.html
blob: bd53ca7d85843dfe58bbedbcfd2b0b225b102de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/object.doc:86 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Timers</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>



<p> <a href="tqobject.html">TQObject</a>, the base class of all TQt objects, provides the basic timer
support in TQt. With <a href="tqobject.html#startTimer">TQObject::startTimer</a>(), you start a timer with
an <em>interval</em> in milliseconds as argument. The function returns a
unique integer timer id. The timer will now "fire" every <em>interval</em>
milliseconds, until you explicitly call <a href="tqobject.html#killTimer">TQObject::killTimer</a>() with
the timer id.
<p> For this mechanism to work, the application must run in an event
loop. You start an event loop with <a href="ntqapplication.html#exec">TQApplication::exec</a>(). When a
timer fires, the application sends a <a href="tqtimerevent.html">TQTimerEvent</a>, and the flow of
control leaves the event loop until the timer event is processed. This
implies that a timer cannot fire while your application is busy doing
something else. In other words: the accuracy of timers depends on the
granularity of your application.
<p> There is practically no upper limit for the interval value (more than
one year is possible). The accuracy depends on the underlying operating
system. Windows 95/98 has 55 millisecond (18.2 times per second)
accuracy; other systems that we have tested (UNIX X11 and Windows NT)
can handle 1 millisecond intervals.
<p> The main API for the timer functionality is <a href="tqtimer.html">TQTimer</a>. That class
provides regular timers that emit a signal when the timer fires, and
inherits <a href="tqobject.html">TQObject</a> so that it fits well into the ownership structure
of most GUI programs. The normal way of using it is like this:
<pre>
    <a href="tqtimer.html">TQTimer</a> * counter = new <a href="tqtimer.html">TQTimer</a>( this );
    connect( counter, TQ_SIGNAL(<a href="tqtimer.html#timeout">timeout</a>()),
             this, TQ_SLOT(updateCaption()) );
    counter-&gt;<a href="tqtimer.html#start">start</a>( 1000 );
</pre>
 
<p> The counter timer is made into a child of this widget, so that when
this widget is deleted, the timer is deleted too. Next, its timeout
signal is connected to the slot that will do the work, and finally
it's started.
<p> <a href="tqtimer.html">TQTimer</a> also provides a simple one-shot timer API. <a href="ntqbutton.html">TQButton</a> uses this
to show the button being pressed down and then (0.1 seconds later) be
released when the keyboard is used to "press" a button, for example:
<p> <pre>
    TQTimer::<a href="tqtimer.html#singleShot">singleShot</a>( 100, this, TQ_SLOT(animateTimeout()) );
</pre>
 
<p> 0.1 seconds after this line of code is executed, the same button's
animateTimeout() slot is called.
<p> Here is an outline of a slightly longer example that combines object
communication via signals and slots with a TQTimer object. It
demonstrates how to use timers to perform intensive calculations in a
single-threaded application without blocking the user interface.
<p> <pre>
    // The Mandelbrot class uses a TQTimer to calculate the mandelbrot
    // set one scanline at a time without blocking the CPU. It
    // inherits TQObject to use signals and slots. Calling start()
    // starts the calculation. The done() signal is emitted when it
    // has finished. Note that this example is not complete, just an
    // outline.

    class Mandelbrot : public <a href="tqobject.html">TQObject</a>
    {
        <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a> // required for signals/slots
    public:
        Mandelbrot( <a href="tqobject.html">TQObject</a> *parent=0, const char *name );
        ...
    public slots:
        void start();
    signals:
        void done();
    private slots:
        void calculate();
        private:
        <a href="tqtimer.html">TQTimer</a> timer;
        ...
    };

    //
    // Constructs and initializes a Mandelbrot object.
    //

    Mandelbrot::Mandelbrot( <a href="tqobject.html">TQObject</a> *parent=0, const char *name )
    : <a href="tqobject.html">TQObject</a>( parent, name )
    {
        <a href="tqobject.html#connect">connect</a>( &amp;timer, TQ_SIGNAL(<a href="tqtimer.html#timeout">timeout</a>()), TQ_SLOT(calculate()) );
        ...
    }

    //
    // Starts the calculation task. The internal calculate() slot
    // will be activated every 10 milliseconds.
    //

    void Mandelbrot::start()
    {
        if ( !timer.<a href="tqtimer.html#isActive">isActive</a>() ) // not already running
            timer.<a href="tqtimer.html#start">start</a>( 10 );   // timeout every 10 ms
    }

    //
    // Calculates one scanline at a time.
    // Emits the done() signal when finished.
    //

    void Mandelbrot::calculate()
    {
        ...                // perform the calculation for a scanline
        if ( finished ) {  // no more scanlines
            timer.<a href="tqtimer.html#stop">stop</a>();
            emit done();
        }
    }
  </pre>
 
<p> 
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>