Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 For this mechanism to work, the application must run in an event
19 loop. You cat 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 main API for the timer functionality was \l QTimer. QTimer stores
35 the interval in a signed integer, which limits the maximum interval it
36 supports to the number of milliseconds that can fit in a signed integer
37 (in practice, this is a period of around 24 days).
38
39 Qt 6.8 introduced the \l QChronoTimer class to replace QTimer. QChronoTimer
40 stores the interval as \c std::chrono::nanoseconds, which means that
41 the maximum interval it supports is around 292 years. This mitigates
42 the chances of integer overflow that QTimer had if the interval was more
43 than \c{std::numeric_limits<int>::max()}.
44
45 The accuracy of the timers depends on the underlying operating system.
46 Windows 2000 has 15ms accuracy; other systems that we have tested can
47 handle 1ms intervals.
48
49 QChronoTimer provides regular timers that emit a signal when the timer
50 fires, and inherits from QObject so that it fits well into the ownership
51 structure of most Qt programs. The normal way of using it is like this:
52
53 \snippet timers/timers.cpp timer-interval-in-ctor
54 \snippet timers/timers.cpp timer-setinterval
55
56 The QChronoTimer object is made into a child of the \c this object so
57 that, when \c this is destroyed, the timer is destroyed too. Next, the
58 \l{QChronoTimer::}{timeout()} signal is connected to the slot that will
59 do the work, the timer interval can be either passed to the constructor,
60 or set later on with setInterval().
61
62 QChronoTimer also provides static functions for single-shot timers.
63 For example:
64
65 \snippet timers/timers.cpp qchronotimer-singleshot
66
67 200ms after this line of code is executed, the \c updateCaption() slot
68 will be called.
69
70 For QChronoTimer to work, you must have an event loop in your application;
71 that is, you must call QCoreApplication::exec() somewhere. Timer events
72 will be delivered only while the event loop is running.
73
74 In multithreaded applications, you can use QChronoTimer in any thread
75 that has an event loop. To start an event loop from a non-GUI thread, use
76 QThread::exec(). Qt uses the timer's \l{QObject::thread()}{thread affinity}
77 to determine which thread will emit the \l{QChronoTimer::}{timeout()}
78 signal. Because of this, you must start and stop the timer in its thread;
79 it is not possible to start a timer from another thread.
80
81 The \l{widgets/analogclock}{Analog Clock} example shows how to
82 use QChronoTimer to redraw a widget at regular intervals. From
83 \c{AnalogClock}'s implementation:
84
85 \snippet timers/analogclock.cpp analogclock-qchronotimer
86
87 Every second, QChronoTimer will call the QWidget::update() slot to
88 refresh the clock's display.
89
90 If you already have a QObject subclass and want an easy
91 optimization, you can use QBasicTimer instead of QTimer. With
92 QBasicTimer, you must reimplement
93 \l{QObject::timerEvent()}{timerEvent()} in your QObject subclass
94 and handle the timeout there.
95*/