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

  1. Qt5 Qtimer
  2. Qtimer Signal Slot Examples

Connecting in Qt 5. There are several ways to connect a signal in Qt 5. Qt 5 continues to support the old string-based syntax for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget). Connect( sender, SIGNAL( valueChanged( QString, QString ) ), receiver, SLOT( updateValue( QString ) ) ). 8BiTs 매일 코딩 홈페이지.

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:

Qt5 qtimer

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

reference resources: QTimer timer

Example

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.

Qt5 Qtimer

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

Qtimer Signal Slot Examples

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:

Qtimer start

reference resources: Qtimer of Qt

Coments are closed
Scroll to top