You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/html/timers.html

153 lines
6.2 KiB

<!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="qobject.html">TQObject</a>, the base class of all TQt objects, provides the basic timer
support in TQt. With <a href="qobject.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="qobject.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="qapplication.html#exec">TQApplication::exec</a>(). When a
timer fires, the application sends a <a href="qtimerevent.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="qtimer.html">TQTimer</a>. That class
provides regular timers that emit a signal when the timer fires, and
inherits <a href="qobject.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="qtimer.html">TQTimer</a> * counter = new <a href="qtimer.html">TQTimer</a>( this );
connect( counter, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()),
this, SLOT(updateCaption()) );
counter-&gt;<a href="qtimer.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="qtimer.html">TQTimer</a> also provides a simple one-shot timer API. <a href="qbutton.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="qtimer.html#singleShot">singleShot</a>( 100, this, 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="qobject.html">TQObject</a>
{
<a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> // retquired for signals/slots
public:
Mandelbrot( <a href="qobject.html">TQObject</a> *parent=0, const char *name );
...
public slots:
void start();
signals:
void done();
private slots:
void calculate();
private:
<a href="qtimer.html">TQTimer</a> timer;
...
};
//
// Constructs and initializes a Mandelbrot object.
//
Mandelbrot::Mandelbrot( <a href="qobject.html">TQObject</a> *parent=0, const char *name )
: <a href="qobject.html">TQObject</a>( parent, name )
{
<a href="qobject.html#connect">connect</a>( &amp;timer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), SLOT(calculate()) );
...
}
//
// Starts the calculation task. The internal calculate() slot
// will be activated every 10 milliseconds.
//
void Mandelbrot::start()
{
if ( !timer.<a href="qtimer.html#isActive">isActive</a>() ) // not already running
timer.<a href="qtimer.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="qtimer.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>