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 cat start an event loop with QApplication::exec(). When a
29 timer fires, the application sends a QTimerEvent, and the flow of
30 control leaves the event loop until the timer event is processed.
31 This implies that a timer cannot fire while your application is
32 busy doing something else. In other words: the accuracy of timers
33 depends on the granularity of your application.
34
35 In multithreaded applications, you can use the timer mechanism in
36 any thread that has an event loop. To start an event loop from a
37 non-GUI thread, use QThread::exec(). Qt uses the object's
38 \l{QObject::thread()}{thread affinity} to determine which thread
39 will deliver the QTimerEvent. Because of this, you must start and
40 stop all timers in the object's thread; it is not possible to
41 start timers for objects in another thread.
42
43 The main API for the timer functionality is \l QTimer. QTimer stores
44 the interval in a signed integer, which limits the maximum interval it
45 supports to the number of milliseconds that can fit in a signed integer
46 (in practice, this is a period of around 24 days).
47
48 Qt 6.8 introduced the \l QChronoTimer class. The main difference between
49 the two classes is that QChronoTimer supports a larger interval range
50 and a higher precision (\c std::chrono::nanoseconds). For QTimer the
51 maximum supported interval is ±24 days, whereas for QChronoTimer it is
52 ±292 years. If you only need millisecond resolution and ±24 days range,
53 you can continue to use \l QTimer. Note that QChronoTimer exists mainly
54 because QTimer's precision couldn't be changed to \c std::chrono::nanoseconds
55 without breaking binary-compatibility.
56
57 The accuracy of the timers depends on the underlying operating system.
58 Windows 2000 has 15ms accuracy; other systems that we have tested can
59 handle 1ms intervals.
60
61 QTimer provides regular timers that emit a signal when the timer
62 fires, and inherits from QObject so that it fits well into the ownership
63 structure of most Qt programs. The normal way of using it is like this:
64
65 \snippet timers/timers.cpp timer-interval-in-ctor
66 \snippet timers/timers.cpp timer-setinterval
67
68 The QTimer object is made into a child of the \c this object so
69 that, when \c this is destroyed, the timer is destroyed too. Next, the
70 \l{QTimer::}{timeout()} signal is connected to the slot that will
71 do the work, the timer interval can be either passed to the constructor,
72 or set later on with setInterval().
73
74 QTimer also provides static functions for single-shot timers.
75 For example:
76
77 \snippet timers/timers.cpp qtimer-singleshot
78
79 200ms after this line of code is executed, the \c updateCaption() slot
80 will be called.
81
82 For QTimer to work, you must have an event loop in your application;
83 that is, you must call QCoreApplication::exec() somewhere. Timer events
84 will be delivered only while the event loop is running.
85
86 In multithreaded applications, you can use QTimer in any thread
87 that has an event loop. To start an event loop from a non-GUI thread, use
88 QThread::exec(). Qt uses the timer's \l{QObject::thread()}{thread affinity}
89 to determine which thread will emit the \l{QTimer::}{timeout()}
90 signal. Because of this, you must start and stop the timer in its thread;
91 it is not possible to start a timer from another thread.
92
93 The \l{widgets/analogclock}{Analog Clock} example shows how to
94 use QTimer to redraw a widget at regular intervals. From
95 \c{AnalogClock}'s implementation:
96
97 \snippet timers/analogclock.cpp analogclock-qtimer
98
99 Every second, QTimer will call the QWidget::update() slot to
100 refresh the clock's display.
101*/