1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the documentation of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
46 \brief How to use timers in your application.
48 QObject, the base class of all Qt objects, provides the basic
49 timer support in Qt. With QObject::startTimer(), you start a
50 timer with an interval in milliseconds as argument. The function
51 returns a unique integer timer ID. The timer will now fire at
52 regular intervals until you explicitly call QObject::killTimer()
55 For this mechanism to work, the application must run in an event
56 loop. You start an event loop with QApplication::exec(). When a
57 timer fires, the application sends a QTimerEvent, and the flow of
58 control leaves the event loop until the timer event is processed.
59 This implies that a timer cannot fire while your application is
60 busy doing something else. In other words: the accuracy of timers
61 depends on the granularity of your application.
63 In multithreaded applications, you can use the timer mechanism in
64 any thread that has an event loop. To start an event loop from a
65 non-GUI thread, use QThread::exec(). Qt uses the object's
66 \l{QObject::thread()}{thread affinity} to determine which thread
67 will deliver the QTimerEvent. Because of this, you must start and
68 stop all timers in the object's thread; it is not possible to
69 start timers for objects in another thread.
71 The upper limit for the interval value is determined by the number
72 of milliseconds that can be specified in a signed integer
73 (in practice, this is a period of just over 24 days). The accuracy
74 depends on the underlying operating system. Windows 98 has 55
75 millisecond accuracy; other systems that we have tested can handle
76 1 millisecond intervals.
78 The main API for the timer functionality is QTimer. That class
79 provides regular timers that emit a signal when the timer fires, and
80 inherits QObject so that it fits well into the ownership structure
81 of most GUI programs. The normal way of using it is like this:
83 \snippet doc/src/snippets/timers/timers.cpp 0
84 \snippet doc/src/snippets/timers/timers.cpp 1
85 \snippet doc/src/snippets/timers/timers.cpp 2
87 The QTimer object is made into a child of this widget so that,
88 when this widget is deleted, the timer is deleted too.
89 Next, its \l{QTimer::}{timeout()} signal is connected to the slot
90 that will do the work, it is started with a value of 1000
91 milliseconds, indicating that it will time out every second.
93 QTimer also provides a static function for single-shot timers.
96 \snippet doc/src/snippets/timers/timers.cpp 3
98 200 milliseconds (0.2 seconds) after this line of code is
99 executed, the \c updateCaption() slot will be called.
101 For QTimer to work, you must have an event loop in your
102 application; that is, you must call QCoreApplication::exec()
103 somewhere. Timer events will be delivered only while the event
106 In multithreaded applications, you can use QTimer in any thread
107 that has an event loop. To start an event loop from a non-GUI
108 thread, use QThread::exec(). Qt uses the timer's
109 \l{QObject::thread()}{thread affinity} to determine which thread
110 will emit the \l{QTimer::}{timeout()} signal. Because of this, you
111 must start and stop the timer in its thread; it is not possible to
112 start a timer from another thread.
114 The \l{widgets/analogclock}{Analog Clock} example shows how to use
115 QTimer to redraw a widget at regular intervals. From \c{AnalogClock}'s
118 \snippet examples/widgets/analogclock/analogclock.cpp 0
119 \snippet examples/widgets/analogclock/analogclock.cpp 2
120 \snippet examples/widgets/analogclock/analogclock.cpp 3
121 \snippet examples/widgets/analogclock/analogclock.cpp 4
122 \snippet examples/widgets/analogclock/analogclock.cpp 5
123 \snippet examples/widgets/analogclock/analogclock.cpp 6
125 \snippet examples/widgets/analogclock/analogclock.cpp 7
127 Every second, QTimer will call the QWidget::update() slot to
128 refresh the clock's display.
130 If you already have a QObject subclass and want an easy
131 optimization, you can use QBasicTimer instead of QTimer. With
132 QBasicTimer, you must reimplement
133 \l{QObject::timerEvent()}{timerEvent()} in your QObject subclass
134 and handle the timeout there. The \l{widgets/wiggly}{Wiggly}
135 example shows how to use QBasicTimer.