Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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 how-to
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 integral timer ID. The timer will then fire at
15
regular intervals until you explicitly call QObject::killTimer()
16
with that timer ID.
17
18
Instead of handling timer IDs directly, you can use \l QBasicTimer.
19
QBasicTimer is a value-class,
20
\l{http://en.cppreference.com/w/cpp/language/raii}{RAII} wrapper around
21
a timer ID. You start the timer with \l{QBasicTimer::start()}, and stop
22
it with \l{QBasicTimer::stop()} (the latter is also called upon destruction).
23
To use QBasicTimer, you must reimplement \l{QObject::timerEvent()}{timerEvent()}
24
in your class (which must be a sub-class of QObject), and handle the
25
timer event there.
26
27
For this mechanism to work, the application must run in an event
28
loop. You can start an event loop with QCoreApplication::exec(). For
29
\l {Qt Widgets} applications, see QApplication::exec(). When a
30
timer fires, the application sends a QTimerEvent, and the flow of
31
control leaves the event loop until the timer event is processed.
32
This implies that a timer cannot fire while your application is
33
busy doing something else. In other words: the accuracy of timers
34
depends on the granularity of your application. The accuracy of timers also
35
depends on the underlying operating system and hardware. Short intervals may
36
be rounded or delayed by the system scheduler, and all timer types may time
37
out later than requested if the system is busy. For more information about
38
timer resolution and Qt::TimerType, see the QTimer documentation.
39
40
In multithreaded applications, you can use the timer mechanism in
41
any thread that has an event loop. To start an event loop from a
42
non-GUI thread, use QThread::exec(). Qt uses the object's
43
\l{QObject::thread()}{thread affinity} to determine which thread
44
will deliver the QTimerEvent. Because of this, you must start and
45
stop all timers in the object's thread; it is not possible to
46
start timers for objects in another thread.
47
48
The main API for the timer functionality is \l QTimer. QTimer stores
49
the interval in a signed integer, which limits the maximum interval it
50
supports to the number of milliseconds that can fit in a signed integer
51
(in practice, this is a period of around 24 days).
52
53
Qt 6.8 introduced the \l QChronoTimer class. The main difference between
54
the two classes is that QChronoTimer supports a larger interval range
55
and a higher precision (\c std::chrono::nanoseconds). For QTimer the
56
maximum supported interval is ±24 days, whereas for QChronoTimer it is
57
±292 years. If you only need millisecond resolution and ±24 days range,
58
you can continue to use \l QTimer. Note that QChronoTimer exists mainly
59
because QTimer's precision couldn't be changed to \c std::chrono::nanoseconds
60
without breaking binary-compatibility.
61
62
QTimer provides regular timers that emit a signal when the timer
63
fires, and inherits from QObject so that it fits well into the ownership
64
structure of most Qt programs. The normal way of using it is like this:
65
66
\snippet timers/timers.cpp timer-interval-in-ctor
67
\snippet timers/timers.cpp timer-setinterval
68
69
The QTimer object is made into a child of the \c this object so
70
that, when \c this is destroyed, the timer is destroyed too. Next, the
71
\l{QTimer::}{timeout()} signal is connected to the slot that will
72
do the work, the timer interval can be either passed to the constructor,
73
or set later on with setInterval().
74
75
QTimer also provides static functions for single-shot timers.
76
For example:
77
78
\snippet timers/timers.cpp qtimer-singleshot
79
80
200ms after this line of code is executed, the \c updateCaption() slot
81
will be called.
82
83
For QTimer to work, you must have an event loop in your application;
84
that is, you must call QCoreApplication::exec() somewhere. Timer events
85
will be delivered only while the event loop is running.
86
87
In multithreaded applications, you can use QTimer in any thread
88
that has an event loop. To start an event loop from a non-GUI thread, use
89
QThread::exec(). Qt uses the timer's \l{QObject::thread()}{thread affinity}
90
to determine which thread will emit the \l{QTimer::}{timeout()}
91
signal. Because of this, you must start and stop the timer in its thread;
92
it is not possible to start a timer from another thread.
93
94
The \l{widgets/analogclock}{Analog Clock} example shows how to
95
use QTimer to redraw a widget at regular intervals. From
96
\c{AnalogClock}'s implementation:
97
98
\snippet timers/analogclock.cpp analogclock-qtimer
99
100
Every second, QTimer will call the QWidget::update() slot to
101
refresh the clock's display.
102
*/
qtbase
src
corelib
doc
src
timers.qdoc
Generated on
for Qt by
1.16.1