1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "qabstracteventdispatcher.h"
32
#include "qcoreapplication.h"
36
\brief The QTimer class provides repetitive and single-shot timers.
42
The QTimer class provides a high-level programming interface for
43
timers. To use it, create a QTimer, connect its timeout() signal
44
to the appropriate slots, and call start(). From then on it will
45
emit the timeout() signal at constant intervals.
47
Example for a one second (1000 millisecond) timer (from the
48
\l{widgets/analogclock}{Analog Clock} example):
50
\quotefromfile widgets/analogclock/analogclock.cpp
54
\printline start(1000)
56
From then on, the \c update() slot is called every second.
58
You can set a timer to time out only once by calling
59
setSingleShot(true). You can also use the static
60
QTimer::singleShot() function to call a slot after a specified
63
\quotefromfile snippets/timers/timers.cpp
67
As a special case, a QTimer with a timeout of 0 will time out as
68
soon as all the events in the window system's event queue have
69
been processed. This can be used to do heavy work while providing
70
a snappy user interface:
74
\printline = new QTimer
78
\c processOneThing() will from then on be called repeatedly. It
79
should be written in such a way that it always returns quickly
80
(typically after processing one data item) so that Qt can deliver
81
events to widgets and stop the timer as soon as it has done all
82
its work. This is the traditional way of implementing heavy work
83
in GUI applications; multithreading is now becoming available on
84
more and more platforms, and we expect that zero-milliseconds
85
QTimers will gradually be replaced by \l{QThread}s.
87
Note that QTimer's accuracy depends on the underlying operating
88
system and hardware. Most platforms support an accuracy of 1
89
milliseconds, but Windows 95 and 98 support only 55. If Qt is
90
unable to deliver the requested number of timer clicks, it will
91
silently discard some.
93
An alternative to using QTimer is to call QObject::startTimer()
94
for your object and reimplement the QObject::timerEvent() event
95
handler in your class (which must inherit QObject). The
96
disadvantage is that timerEvent() does not support such
97
high-level features as single-shot timers or signals.
99
Another alternative to using QTimer is to use QBasicTimer. It is
100
typically less cumbersome than using QObject::startTimer()
101
directly. See \l{timers.html}{Timers} for an overview of all
104
Some operating systems limit the number of timers that may be
105
used; Qt tries to work around these limitations.
107
\sa QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers
111
static const int INV_TIMER = -1; // invalid timer id
114
Constructs a timer with the given \a parent.
117
QTimer::QTimer(QObject *parent)
118
: QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0)
125
Constructs a timer called \a name, with a \a parent.
128
QTimer::QTimer(QObject *parent, const char *name)
129
: QObject(parent), id(INV_TIMER), single(0), nulltimer(0)
131
setObjectName(QString::fromAscii(name));
141
if (id != INV_TIMER) // stop running timer
147
\fn void QTimer::timeout()
149
This signal is emitted when the timer times out.
151
\sa interval, start(), stop()
155
\fn bool QTimer::isActive() const
157
Returns true if the timer is running (pending); otherwise returns
162
\fn int QTimer::timerId() const
164
Returns the ID of the timer if the timer is running; otherwise returns
171
Starts or restarts the timer with the timeout specified in \l interval.
173
If \l singleShot is true, the timer will be activated only once.
177
if (id != INV_TIMER) // stop running timer
179
nulltimer = (!inter && single);
180
id = QObject::startTimer(inter);
184
Starts or restarts the timer with a timeout interval of \a msec
187
void QTimer::start(int msec)
197
Call setSingleShot(\a sshot) and start(\a msec) instead.
200
int QTimer::start(int msec, bool sshot)
202
if (id >=0 && nulltimer && !msec && sshot)
206
setSingleShot(sshot);
221
if (id != INV_TIMER) {
222
QObject::killTimer(id);
231
void QTimer::timerEvent(QTimerEvent *e)
233
if (e->timerId() == id) {
240
class QSingleShotTimer : public QObject
246
QSingleShotTimer(int msec, QObject *r, const char * m);
250
void timerEvent(QTimerEvent *);
253
QSingleShotTimer::QSingleShotTimer(int msec, QObject *receiver, const char *member)
254
: QObject(QAbstractEventDispatcher::instance())
256
connect(this, SIGNAL(timeout()), receiver, member);
257
timerId = startTimer(msec);
260
QSingleShotTimer::~QSingleShotTimer()
266
void QSingleShotTimer::timerEvent(QTimerEvent *)
268
// need to kill the timer _before_ we emit timeout() in case the
269
// slot connected to timeout calls processEvents()
277
#include "qtimer.moc"
281
This static function calls a slot after a given time interval.
283
It is very convenient to use this function because you do not need
284
to bother with a \link QObject::timerEvent() timerEvent\endlink or
285
create a local QTimer object.
289
#include <QApplication>
292
int main(int argc, char *argv[])
294
QApplication app(argc, argv);
295
QTimer::singleShot(60000, &app, SLOT(quit()));
301
This sample program automatically terminates after 10 minutes
302
(600000 milliseconds).
304
The \a receiver is the receiving object and the \a member is the
305
slot. The time interval is \a msec milliseconds.
310
void QTimer::singleShot(int msec, QObject *receiver, const char *member)
312
if (receiver && member)
313
(void) new QSingleShotTimer(msec, receiver, member);
317
\property QTimer::singleShot
318
\brief whether the timer is a single-shot timer
320
A single-shot timer fires only once, non-single-shot timers fire
321
every \l interval milliseconds.
323
\sa interval, singleShot()
327
\property QTimer::interval
328
\brief the timeout interval in milliseconds
330
The default value for this property is 0. A QTimer with a timeout
331
interval of 0 will time out as soon as all the events in the window
332
system's event queue have been processed.
334
Setting the interval of an active timer changes its timerId().
338
void QTimer::setInterval(int msec)
341
if (id != INV_TIMER) { // create new timer
342
QObject::killTimer(id); // restart timer
343
id = QObject::startTimer(msec);
347
/*! \fn void QTimer::changeInterval(int msec)
349
Use setInterval(msec) or start(msec) instead.