QTimer¶
Synopsis¶
Functions¶
def
interval
()def
isActive
()def
isSingleShot
()def
remainingTime
()def
setInterval
(msec)def
setSingleShot
(singleShot)def
setTimerType
(atype)def
timerId
()def
timerType
()
Static functions¶
def
singleShot
(arg__1, arg__2)def
singleShot
(msec, receiver, member)def
singleShot
(msec, timerType, receiver, member)
Detailed Description¶
The
QTimer
class provides a high-level programming interface for timers. To use it, create aQTimer
, connect itstimeout()
signal to the appropriate slots, and callstart()
. From then on, it will emit thetimeout()
signal at constant intervals.Example for a one second (1000 millisecond) timer (from the Analog Clock example):
QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000);From then on, the
update()
slot is called every second.You can set a timer to time out only once by calling
setSingleShot
(true). You can also use the staticsingleShot()
function to call a slot after a specified interval:QTimer.singleShot(200, self.updateCaption)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.As a special case, a
QTimer
with a timeout of 0 will time out as soon as all the events in the window system’s event queue have been processed. This can be used to do heavy work while providing a snappy user interface:timer = QTimer(self) timer.timeout.connect(self.processOneThing) timer.start()From then on,
processOneThing()
will be called repeatedly. It should be written in such a way that it always returns quickly (typically after processing one data item) so that Qt can deliver events to the user interface and stop the timer as soon as it has done all its work. This is the traditional way of implementing heavy work in GUI applications, but as multithreading is nowadays becoming available on more and more platforms, we expect that zero-millisecondQTimer
objects will gradually be replaced byQThread
s.
Accuracy and Timer Resolution¶
The accuracy of timers depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.
The accuracy also depends on the
timer type
. ForPreciseTimer
,QTimer
will try to keep the accuracy at 1 millisecond. Precise timers will also never time out earlier than expected.For
CoarseTimer
andVeryCoarseTimer
types,QTimer
may wake up earlier than expected, within the margins for those types: 5% of the interval forCoarseTimer
and 500 ms forVeryCoarseTimer
.All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy. In such a case of timeout overrun, Qt will emit
timeout()
only once, even if multiple timeouts have expired, and then will resume the original interval.
Alternatives to QTimer¶
An alternative to using
QTimer
is to callstartTimer()
for your object and reimplement thetimerEvent()
event handler in your class (which must inheritQObject
). The disadvantage is thattimerEvent()
does not support such high-level features as single-shot timers or signals.Another alternative is
QBasicTimer
. It is typically less cumbersome than usingstartTimer()
directly. See Timers for an overview of all three approaches.Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations.
-
PySide2.QtCore.QTimer.
interval
()¶ - Return type
int
See also
-
PySide2.QtCore.QTimer.
isActive
()¶ - Return type
bool
Returns
true
if the timer is running (pending); otherwise returns false.
-
PySide2.QtCore.QTimer.
isSingleShot
()¶ - Return type
bool
-
PySide2.QtCore.QTimer.
remainingTime
()¶ - Return type
int
-
PySide2.QtCore.QTimer.
setInterval
(msec)¶ - Parameters
msec –
int
See also
-
PySide2.QtCore.QTimer.
setSingleShot
(singleShot)¶ - Parameters
singleShot –
bool
See also
-
PySide2.QtCore.QTimer.
setTimerType
(atype)¶ - Parameters
atype –
TimerType
See also
-
static
PySide2.QtCore.QTimer.
singleShot
(arg__1, arg__2)¶ - Parameters
arg__1 –
int
arg__2 –
PyCallable
-
static
PySide2.QtCore.QTimer.
singleShot
(msec, timerType, receiver, member) - Parameters
msec –
int
timerType –
TimerType
receiver –
QObject
member – str
This is an overloaded function.
This static function calls a slot after a given time interval.
It is very convenient to use this function because you do not need to bother with a
timerEvent
or create a localQTimer
object.The
receiver
is the receiving object and themember
is the slot. The time interval ismsec
milliseconds. ThetimerType
affects the accuracy of the timer.See also
-
static
PySide2.QtCore.QTimer.
singleShot
(msec, receiver, member) - Parameters
msec –
int
receiver –
QObject
member – str
This static function calls a slot after a given time interval.
It is very convenient to use this function because you do not need to bother with a
timerEvent
or create a localQTimer
object.Example:
from PySide2.QtCore import QApplication, QTimer def main(): app = QApplication([]) QTimer.singleShot(600000, app, SLOT('quit()')) ... return app.exec_()
This sample program automatically terminates after 10 minutes (600,000 milliseconds).
The
receiver
is the receiving object and themember
is the slot. The time interval ismsec
milliseconds.See also
-
PySide2.QtCore.QTimer.
start
()¶ This function overloads .
Starts or restarts the timer with the timeout specified in
interval
.If the timer is already running, it will be
stopped
and restarted.If
singleShot
is true, the timer will be activated only once.
-
PySide2.QtCore.QTimer.
start
(msec) - Parameters
msec –
int
Starts or restarts the timer with a timeout interval of
msec
milliseconds.If the timer is already running, it will be
stopped
and restarted.If
singleShot
is true, the timer will be activated only once.
-
PySide2.QtCore.QTimer.
timerId
()¶ - Return type
int
Returns the ID of the timer if the timer is running; otherwise returns -1.
-
PySide2.QtCore.QTimer.
timerType
()¶ - Return type
TimerType
See also
© 2018 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.