1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
31 \brief How to use Qt timers in your application.
33 \ingroup best-practices
35 QObject, the base class of all Qt objects, provides the basic
36 timer support in Qt. With QObject::startTimer(), you start a
37 timer with an interval in milliseconds as argument. The function
38 returns a unique integer timer ID. The timer will now fire at
39 regular intervals until you explicitly call QObject::killTimer()
42 For this mechanism to work, the application must run in an event
43 loop. You start an event loop with QApplication::exec(). When a
44 timer fires, the application sends a QTimerEvent, and the flow of
45 control leaves the event loop until the timer event is processed.
46 This implies that a timer cannot fire while your application is
47 busy doing something else. In other words: the accuracy of timers
48 depends on the granularity of your application.
50 In multithreaded applications, you can use the timer mechanism in
51 any thread that has an event loop. To start an event loop from a
52 non-GUI thread, use QThread::exec(). Qt uses the object's
53 \l{QObject::thread()}{thread affinity} to determine which thread
54 will deliver the QTimerEvent. Because of this, you must start and
55 stop all timers in the object's thread; it is not possible to
56 start timers for objects in another thread.
58 The upper limit for the interval value is determined by the number
59 of milliseconds that can be specified in a signed integer
60 (in practice, this is a period of just over 24 days). The accuracy
61 depends on the underlying operating system. Windows 2000 has 15
62 millisecond accuracy; other systems that we have tested can handle
63 1 millisecond intervals.
65 The main API for the timer functionality is QTimer. That class
66 provides regular timers that emit a signal when the timer fires, and
67 inherits QObject so that it fits well into the ownership structure
68 of most GUI programs. The normal way of using it is like this:
70 \snippet timers/timers.cpp 0
71 \snippet timers/timers.cpp 1
72 \snippet timers/timers.cpp 2
74 The QTimer object is made into a child of this widget so that,
75 when this widget is deleted, the timer is deleted too.
76 Next, its \l{QTimer::}{timeout()} signal is connected to the slot
77 that will do the work, it is started with a value of 1000
78 milliseconds, indicating that it will time out every second.
80 QTimer also provides a static function for single-shot timers.
83 \snippet timers/timers.cpp 3
85 200 milliseconds (0.2 seconds) after this line of code is
86 executed, the \c updateCaption() slot will be called.
88 For QTimer to work, you must have an event loop in your
89 application; that is, you must call QCoreApplication::exec()
90 somewhere. Timer events will be delivered only while the event
93 In multithreaded applications, you can use QTimer in any thread
94 that has an event loop. To start an event loop from a non-GUI
95 thread, use QThread::exec(). Qt uses the timer's
96 \l{QObject::thread()}{thread affinity} to determine which thread
97 will emit the \l{QTimer::}{timeout()} signal. Because of this, you
98 must start and stop the timer in its thread; it is not possible to
99 start a timer from another thread.
101 The \l{widgets/analogclock}{Analog Clock} example shows how to use
102 QTimer to redraw a widget at regular intervals. From \c{AnalogClock}'s
105 \snippet analogclock.cpp 0
106 \snippet analogclock.cpp 2
107 \snippet analogclock.cpp 3
108 \snippet analogclock.cpp 4
109 \snippet analogclock.cpp 5
110 \snippet analogclock.cpp 6
112 \snippet analogclock.cpp 7
114 Every second, QTimer will call the QWidget::update() slot to
115 refresh the clock's display.
117 If you already have a QObject subclass and want an easy
118 optimization, you can use QBasicTimer instead of QTimer. With
119 QBasicTimer, you must reimplement
120 \l{QObject::timerEvent()}{timerEvent()} in your QObject subclass
121 and handle the timeout there. The \l{widgets/wiggly}{Wiggly}
122 example shows how to use QBasicTimer.