PySide6.QtCore.QTimer¶
- class QTimer¶
- The - QTimerclass provides repetitive and single-shot timers. More…- Synopsis¶- Properties¶
- intervalᅟ- The timeout interval in milliseconds
- remainingTimeᅟ- The remaining time in milliseconds
- singleShotᅟ- Whether the timer is a single-shot timer
- timerTypeᅟ- Controls the accuracy of the timer
 - Methods¶- def - __init__()
- def - id()
- def - interval()
- def - isActive()
- def - isSingleShot()
- def - remainingTime()
- def - setInterval()
- def - setSingleShot()
- def - setTimerType()
- def - timerId()
- def - timerType()
 - Slots¶- Signals¶- def - timeout()
 - Static functions¶- def - singleShot()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - The - QTimerclass provides a high-level programming interface for timers. To use it, create a- QTimer, connect its- timeout()signal to the appropriate slots, and call- start(). From then on, it will emit the- timeout()signal at constant intervals.- Example for a one second (1000 millisecond) timer (from the Analog Clock example): - timer = QTimer(self) timer.timeout.connect(this, QOverload<>::of(&AnalogClock::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 static- singleShot()function to call a slot after a specified interval:- QTimer::singleShot(200, self.updateCaption) - In multithreaded applications, you can use - QTimerin any thread that has an event loop. To start an event loop from a non-GUI thread, use- exec(). Qt uses the timer’s- thread affinityto determine which thread will emit the- timeout()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 - QTimerwith a timeout of 0 will time out as soon as possible, though the ordering between zero timers and other sources of events is unspecified. Zero timers can be used to do some work while still 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-millisecond- QTimerobjects will gradually be replaced by- QThreads.- 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. For- PreciseTimer,- QTimerwill try to keep the accuracy at 1 millisecond. Precise timers will also never time out earlier than expected.- For - CoarseTimerand- VeryCoarseTimertypes,- QTimermay wake up earlier than expected, within the margins for those types: 5% of the interval for- CoarseTimerand 500 ms for- VeryCoarseTimer.- 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¶- Qt 6.8 introduced - QChronoTimer. The main difference between the two classes, is that- QChronoTimersupports a larger interval range and a higher precision (- std::chrono::nanoseconds). For- QTimerthe maximum supported interval is ±24 days, whereas for- QChronoTimerit is ±292 years (less chances of interger overflow with intervals longer than- std::numeric_limits<int>::max()). If you only need millisecond resolution and ±24 days range, you can continue to use- QTimer.- Another alternative is reimplementing the - timerEvent()method in your class (which must be a sub-class of- QObject), and using one of the following approaches:- Using - QBasicTimer, a lightweight value-class wrapping a timer ID. You can start the timer with QBasicTimer::start() and stop it with- stop(). You can handle the event in your reimplemneted- timerEvent().
- A more low-level method is manipulating the timer IDs directly. To start the timer call - startTimer(), storing the returned ID. To stop the timer call- killTimer(). You can handle the event in your reimplemented- timerEvent(). This approach is typically more cumbersome than using- QBasicTimer.
 - A disadvantage of using - timerEvent()is that some high-level features, such as single-shot timers and signals, aren’t supported.- Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations. - See also - QBasicTimer- QTimerEvent- timerEvent()Timers- Analog Clock- Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property activeᅟ: bool¶
 - This boolean property is - trueif the timer is running; otherwise false.- Access functions:
 - property intervalᅟ: int¶
 - This property holds the timeout interval in milliseconds. - The default value for this property is 0. A - QTimerwith a timeout interval of 0 will time out as soon as all the events in the window system’s event queue have been processed.- Setting the interval of a running timer will change the interval, - stop()and then- start()the timer, and acquire a new- id(). If the timer is not running, only the interval is changed.- See also - Access functions:
 - property remainingTimeᅟ: int¶
 - This property holds the remaining time in milliseconds. - Returns the timer’s remaining value in milliseconds left until the timeout. If the timer is inactive, the returned value will be -1. If the timer is overdue, the returned value will be 0. - See also - Access functions:
 - property singleShotᅟ: bool¶
 - This property holds whether the timer is a single-shot timer. - A single-shot timer fires only once, non-single-shot timers fire every - intervalmilliseconds.- The default value for this property is - false.- See also - Access functions:
 - property timerTypeᅟ: Qt.TimerType¶
 - This property holds controls the accuracy of the timer. - The default value for this property is - Qt::CoarseTimer.- See also - Access functions:
 - Constructs a timer with the given - parent.- Returns a - TimerIdrepresenting the timer ID if the timer is running; otherwise returns- Qt::TimerId::Invalid.- See also - TimerId- interval()¶
- Return type:
- int 
 - See also 
 - Getter of property - intervalᅟ.- isActive()¶
- Return type:
- bool 
 
 - Returns - trueif the timer is running; otherwise returns- false.- Getter of property - activeᅟ.- isSingleShot()¶
- Return type:
- bool 
 
 - Getter of property - singleShotᅟ.- remainingTime()¶
- Return type:
- int 
 
 - Getter of property - remainingTimeᅟ.- setInterval(msec)¶
- Parameters:
- msec – int 
 - See also 
 - Setter of property - intervalᅟ.- setSingleShot(singleShot)¶
- Parameters:
- singleShot – bool 
 - See also 
 - Setter of property - singleShotᅟ.- Setter of property - timerTypeᅟ.- static singleShot(msec, functor)¶
- Parameters:
- msec – int 
- functor – - PyCallable
 
 
 - static singleShot(msec, context, functor)
- Parameters:
- msec – int 
- context – - QObject
- functor – - PyCallable
 
 
 - static singleShot(msec, receiver, member)
- Parameters:
- msec – int 
- receiver – - QObject
- member – str 
 
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Use the chrono overloads. 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 - timerEventor create a local- QTimerobject.- Example: - from PySide6.QtWidgets import QApplication from PySide6.QtCore import QTimer if __name__ == "__main__": app = QApplication([]) QTimer.singleShot(600000, app, QCoreApplication.quit) ... sys.exit(app.exec()) - This sample program automatically terminates after 10 minutes (600,000 milliseconds). - The - receiveris the receiving object and the- memberis the slot. The time interval is- msecmilliseconds.- See also - static singleShot(msec, timerType, receiver, member)
 - This is an overloaded function. - Use the chrono overloads. 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 - timerEventor create a local- QTimerobject.- The - receiveris the receiving object and the- memberis the slot. The time interval is- msecmilliseconds. The- timerTypeaffects the accuracy of the timer.- See also - start()¶
 - This function overloads start(). - Starts or restarts the timer with the timeout specified in - interval.- If the timer is already running, it will be - stoppedand restarted. This will also change its- id().- If - singleShotis true, the timer will be activated only once.- start(msec)
- Parameters:
- msec – int 
 
 - Starts or restarts the timer with a timeout interval of - msecmilliseconds.- If the timer is already running, it will be - stoppedand restarted. This will also change its- id().- If - singleShotis true, the timer will be activated only once. This is equivalent to:- timer.setInterval(msec); timer.start(); - Note - Keeping the event loop busy with a zero-timer is bound to cause trouble and highly erratic behavior of the UI. - stop()¶
 - Stops the timer. - See also - timeout()¶
 - This signal is emitted when the timer times out. - timerId()¶
- Return type:
- int 
 
 - Returns the ID of the timer if the timer is running; otherwise returns -1. - timerType()¶
- Return type:
 - See also 
 - Getter of property - timerTypeᅟ.