Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
timers.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page timers.html
6 \title Timers
7 \brief How to use Qt timers in your application.
8
9 \ingroup best-practices
10
11 QObject, the base class of all Qt objects, provides the basic
12 timer support in Qt. With QObject::startTimer(), you start a
13 timer with an interval in milliseconds as argument. The function
14 returns a unique integer timer ID. The timer will now fire at
15 regular intervals until you explicitly call QObject::killTimer()
16 with the timer ID.
17
18 For this mechanism to work, the application must run in an event
19 loop. You start an event loop with QApplication::exec(). When a
20 timer fires, the application sends a QTimerEvent, and the flow of
21 control leaves the event loop until the timer event is processed.
22 This implies that a timer cannot fire while your application is
23 busy doing something else. In other words: the accuracy of timers
24 depends on the granularity of your application.
25
26 In multithreaded applications, you can use the timer mechanism in
27 any thread that has an event loop. To start an event loop from a
28 non-GUI thread, use QThread::exec(). Qt uses the object's
29 \l{QObject::thread()}{thread affinity} to determine which thread
30 will deliver the QTimerEvent. Because of this, you must start and
31 stop all timers in the object's thread; it is not possible to
32 start timers for objects in another thread.
33
34 The upper limit for the interval value is determined by the number
35 of milliseconds that can be specified in a signed integer
36 (in practice, this is a period of just over 24 days). The accuracy
37 depends on the underlying operating system. Windows 2000 has 15
38 millisecond accuracy; other systems that we have tested can handle
39 1 millisecond intervals.
40
41 The main API for the timer functionality is QTimer. That class
42 provides regular timers that emit a signal when the timer fires, and
43 inherits QObject so that it fits well into the ownership structure
44 of most Qt programs. The normal way of using it is like this:
45
46 \snippet timers/timers.cpp 0
47 \snippet timers/timers.cpp 1
48 \snippet timers/timers.cpp 2
49
50 The QTimer object is made into a child of \c this object so that,
51 when \c this object is deleted, the timer is deleted too.
52 Next, its \l{QTimer::}{timeout()} signal is connected to the slot
53 that will do the work, it is started with a value of 1000
54 milliseconds, indicating that it will time out every second.
55
56 QTimer also provides a static function for single-shot timers.
57 For example:
58
59 \snippet timers/timers.cpp 3
60
61 200 milliseconds (0.2 seconds) after this line of code is
62 executed, the \c updateCaption() slot will be called.
63
64 For QTimer to work, you must have an event loop in your
65 application; that is, you must call QCoreApplication::exec()
66 somewhere. Timer events will be delivered only while the event
67 loop is running.
68
69 In multithreaded applications, you can use QTimer in any thread
70 that has an event loop. To start an event loop from a non-GUI
71 thread, use QThread::exec(). Qt uses the timer's
72 \l{QObject::thread()}{thread affinity} to determine which thread
73 will emit the \l{QTimer::}{timeout()} signal. Because of this, you
74 must start and stop the timer in its thread; it is not possible to
75 start a timer from another thread.
76
77 The \l{widgets/analogclock}{Analog Clock} example shows how to use
78 QTimer to redraw a widget at regular intervals. From \c{AnalogClock}'s
79 implementation:
80
81 \snippet timers/analogclock.cpp 0
82 \snippet timers/analogclock.cpp 2
83 \snippet timers/analogclock.cpp 3
84 \snippet timers/analogclock.cpp 4
85 \snippet timers/analogclock.cpp 5
86 \snippet timers/analogclock.cpp 6
87 \dots
88 \snippet timers/analogclock.cpp 7
89
90 Every second, QTimer will call the QWidget::update() slot to
91 refresh the clock's display.
92
93 If you already have a QObject subclass and want an easy
94 optimization, you can use QBasicTimer instead of QTimer. With
95 QBasicTimer, you must reimplement
96 \l{QObject::timerEvent()}{timerEvent()} in your QObject subclass
97 and handle the timeout there. The \l{widgets/tetrix}{Tetrix}
98 example shows how to use QBasicTimer.
99*/