Timers¶
How to use Qt timers in your application.
QObject
, the base class of all Qt objects, provides the basic timer support in Qt. WithstartTimer()
, you start a timer with an interval in milliseconds as argument. The function returns a unique integer timer ID. The timer will now fire at regular intervals until you explicitly callkillTimer()
with the timer ID.For this mechanism to work, the application must run in an event loop. You start an event loop with
exec()
. When a timer fires, the application sends aQTimerEvent
, and the flow of control leaves the event loop until the timer event is processed. This implies that a timer cannot fire while your application is busy doing something else. In other words: the accuracy of timers depends on the granularity of your application.In multithreaded applications, you can use the timer mechanism in any thread that has an event loop. To start an event loop from a non-GUI thread, use
exec()
. Qt uses the object’sthread affinity
to determine which thread will deliver theQTimerEvent
. Because of this, you must start and stop all timers in the object’s thread; it is not possible to start timers for objects in another thread.The upper limit for the interval value is determined by the number of milliseconds that can be specified in a signed integer (in practice, this is a period of just over 24 days). The accuracy depends on the underlying operating system. Windows 2000 has 15 millisecond accuracy; other systems that we have tested can handle 1 millisecond intervals.
The main API for the timer functionality is
QTimer
. That class provides regular timers that emit a signal when the timer fires, and inheritsQObject
so that it fits well into the ownership structure of most Qt programs. The normal way of using it is like this:timer = QTimer(self) timer.timeout.connect(self.updateCaption) timer.start(1000)The
QTimer
object is made into a child ofthis
object so that, whenthis
object is deleted, the timer is deleted too. Next, itstimeout()
signal is connected to the slot that will do the work, it is started with a value of 1000 milliseconds, indicating that it will time out every second.
QTimer
also provides a static function for single-shot timers. For example:QTimer.singleShot(200, self.updateCaption)200 milliseconds (0.2 seconds) after this line of code is executed, the
updateCaption()
slot will be called.For
QTimer
to work, you must have an event loop in your application; that is, you must callexec()
somewhere. Timer events will be delivered only while the event loop is running.In multithreaded applications, you can use
QTimer
in any thread that has an event loop. To start an event loop from a non-GUI thread, useexec()
. Qt uses the timer’sthread affinity
to determine which thread will emit thetimeout()
signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.The Analog Clock example shows how to use
QTimer
to redraw a widget at regular intervals. FromAnalogClock
‘s implementation:AnalogClock::AnalogClock(QWidget *parent) : QWidget(parent) { QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update)); timer->start(1000); ... }Every second,
QTimer
will call theupdate()
slot to refresh the clock’s display.If you already have a
QObject
subclass and want an easy optimization, you can useQBasicTimer
instead ofQTimer
. WithQBasicTimer
, you must reimplementtimerEvent()
in yourQObject
subclass and handle the timeout there. The Wiggly example shows how to useQBasicTimer
.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.