Qt Qtimer Signal Slot

broken image


In QT5, we have a special timer class, QTimer. We can use it to complete some timing operations. Examples are as follows:

At the end of our timer, we need to stop the timer manually, otherwise it will send out the timeout signal continuously. At the same time, we need to note that before making the connection function, we need to have an instance of the timer, otherwise it will cause the program to crash. Of course, we can use the static method of QTimer instead of instantiating the timer to start the timer directly Here is an example of a QT document:

The connection mechanism uses a vector indexed by signals. But all the slots waste space in the vector and there are usually more slots than signals in an object. So from Qt 4.6, a new internal signal index which only includes the signal index is used. While developing with Qt, you only need to know about the absolute method index.

The signal timeout emitted from main thread, As timer and worker live in different threads, their connection type is queued connection. The slot get called in its living thread, which is the sub-thread. Thanks to a mechanism called queued connections, it is safe to connect signals and slots across different threads. The QTimer class 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).

We can use multiple timers in a program. Each timer has its own timer ID, which is similar to the pid of window process. We can use timer event to process multiple timers

Qt Qtimer Signal Slot

reference resources: QTimer timer

The idea of program design is as follows

  1. Set the timing period first
  2. Bind the timeout() signal to the custom slot function
  3. Call the start function to start the timer

The following is the definition in the widget window class:

In this paper, a QTimer is defined, and the slot function onTimeOut() is defined as the slot function corresponding to the timeout signal.

The code in the constructor of the window class is as follows:

In this example, the timing period of the timer is set to 1s, then the signal is bound to the slot, and finally the timer is started.

The code in the timer processing function is as follows:

reference resources: Qtimer of Qt

Synopsis¶

Functions¶

  • def interval ()
  • def isActive ()
  • def isSingleShot ()
  • def setInterval (msec)
  • def setSingleShot (singleShot)
  • def timerId ()

Signals¶

  • def timeout ()

Static functions¶

  • def singleShot (arg__1, arg__2)
  • def singleShot (msec, receiver, member)

Detailed Description¶

The PySide.QtCore.QTimer class provides repetitive and single-shot timers.

The PySide.QtCore.QTimer class provides a high-level programming interface for timers. To use it, create a PySide.QtCore.QTimer , connect its PySide.QtCore.QTimer.timeout() signal to the appropriate slots, and call PySide.QtCore.QTimer.start() . From then on it will emit the PySide.QtCore.QTimer.timeout() signal at constant intervals.

Example for a one second (1000 millisecond) timer (from the Analog Clock example):

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 QTimer.singleShot() function to call a slot after a specified interval:

In multithreaded applications, you can use PySide.QtCore.QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread.exec() . Qt uses the timer's threadaffinity to determine which thread will emit the PySide.QtCore.QTimer.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 PySide.QtCore.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:

processOneThing() will from then on 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 widgets 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; multithreading is now becoming available on more and more platforms, and we expect that zero-millisecond QTimers will gradually be replaced by PySide.QtCore.QThread s.

Accuracy and Timer Resolution¶

Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

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.

If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.

Alternatives to QTimer¶

An alternative to using PySide.QtCore.QTimer is to call QObject.startTimer() for your object and reimplement the QObject.timerEvent() event handler in your class (which must inherit PySide.QtCore.QObject ). The disadvantage is that PySide.QtCore.QTimer.timerEvent() does not support such high-level features as single-shot timers or signals.

Another alternative to using PySide.QtCore.QTimer is to use PySide.QtCore.QBasicTimer . It is typically less cumbersome than using QObject.startTimer() 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.

See also

PySide.QtCore.QBasicTimerPySide.QtCore.QTimerEventQObject.timerEvent()TimersAnalog Clock ExampleWiggly Example

class PySide.QtCore.QTimer([parent=None]
Parameters:parentPySide.QtCore.QObject

Constructs a timer with the given parent .

PySide.QtCore.QTimer.interval()¶
Return type:PySide.QtCore.int

This property holds the timeout interval in milliseconds.

The default value for this property is 0. A PySide.QtCore.QTimer with 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 an active timer changes its PySide.QtCore.QTimer.timerId() .

PySide.QtCore.QTimer.isActive()¶
Return type:PySide.QtCore.bool

This boolean property is true if the timer is running; otherwise false.

PySide.QtCore.QTimer.isSingleShot()¶
Return type:PySide.QtCore.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 PySide.QtCore.QTimer.interval() milliseconds.

See also

PySide.QtCore.QTimer.setInterval(msec
Parameters:msecPySide.QtCore.int

This property holds the timeout interval in milliseconds.

The default value for this property is 0. A PySide.QtCore.QTimer with 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 an active timer changes its PySide.QtCore.QTimer.timerId() .

PySide.QtCore.QTimer.setSingleShot(singleShot
Parameters:singleShotPySide.QtCore.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 PySide.QtCore.QTimer.interval() milliseconds.

See also

static PySide.QtCore.QTimer.singleShot(msec, receiver, member
Parameters:
  • msecPySide.QtCore.int
  • receiverPySide.QtCore.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 PySide.QtCore.QObject.timerEvent() or create a local PySide.QtCore.QTimer object.

Example:

This sample program automatically terminates after 10 minutes (600,000 milliseconds).

The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.

See also

static PySide.QtCore.QTimer.singleShot(arg__1, arg__2)
Parameters:
  • arg__1PySide.QtCore.int
  • arg__2PyCallable
PySide.QtCore.QTimer.start()¶

This function overloads PySide.QtCore.QTimer.start() .

Starts or restarts the timer with the timeout specified in PySide.QtCore.QTimer.interval() .

If PySide.QtCore.QTimer.singleShot() is true, the timer will be activated only once.

PySide.QtCore.QTimer.start(msec)
Parameters:msecPySide.QtCore.int

Starts or restarts the timer with a timeout interval of msec milliseconds.

PySide.QtCore.QTimer.stop()¶

Stops the timer.

How Qt Signal And Slots Works

PySide.QtCore.QTimer.timeout()¶
PySide.QtCore.QTimer.timerId()¶

Qt Qtimer Signal Slot Machine

Return type:PySide.QtCore.int

Qt Qtimer Signal Slot Adapter

Returns the ID of the timer if the timer is running; otherwise returns -1.





broken image