QT (C): Probe QTimer class

Original Address: https://houkaifa.com/2019/02/20/LearningNotes-QT-03/

Outline

This introduction the basic usage QTimer class, and make use of such a simple countdown procedures.
Environment: Win10 + QT Creator4.4.1 + QT5.9.2.
Implementing content: triggered by a button and a simple countdown displayed on the screen, pop-up boxes after time runs out.

Here Insert Picture Description


A, QTimer Brief Description Class

QTimer class is a class QT clock to us to be, and the periodic signal by creating a connection to the appropriate instance QTimer grooves to easily achieve a periodic function call. The following is the official description given in the manual:

The QTimer class provides repetitive and single-shot timers.
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.

how to use

To use QTimer class must contain the appropriate header files:

#include <QTimer>

After completion of the file containing the header, you can be obtained a pointer to an instance of class QTimer new keywords by:

Wherein the argument is not null constructor specifies the object class QTimer clock binding, generally written the this, and facilitate the establishment of the connection groove of the main window.

QTimer *timer = new QTimer(this); // 创建时钟

After the clock generator, you can by member functions: QTimer :: start (int msec), QTimer :: stop () to control the start and stop the clock:

timer->start(100); // 以 100ms 为时钟周期启动时钟
timer->stop(); // 停止时钟

Of course, the start function parameter list is empty, and therefore can also be a member function QTimer :: setInterval (int msec) to set the clock cycle only, at the right time by QTimer :: start (null) clock started separately, E.g:

QTimer *timer = new QTimer(this); // 创建时钟
timer->setInterval(200); // 设置时钟周期
...
timer->start(); // 启动时钟

Receiving signals

Clock after the operation, each time reaching the set time, it will automatically trigger QTimer :: timeout () signal, a signal by the connect function associated with the slot function, can receive the timing of "Message Clock" of:

QTimer *timer = new QTimer(this); // 创建时钟
timer->setInterval(200);	// 设置时钟周期为 200 ms
connect(timer, SIGNAL(timeout()), this, SLOT(update())); // 将到时信号与主窗口的update槽关联
...
timer->start(); // 启动时钟

Second, the small example: Simple Countdown

Target effect: the text box input time (in seconds), the countdown by pressing a button, the program starts the countdown, and displays the remaining number of seconds on the screen, pop-up message box corresponding prompt when time runs out.

First create a Qt GUI project and build it simple interface:

Here Insert Picture Description

The definition of what timer and the remaining time:

Here Insert Picture Description

PushButton complete realization of the clicked slot, click the button to start the clock:

Here Insert Picture Description

Do not forget to define the groove ah Big Brother:

Here Insert Picture Description

Finally .cpp in the slot to achieve the O98K:

Here Insert Picture Description

The effect of running up like this:

Here Insert Picture Description

Published 26 original articles · won praise 24 · views 30000 +

Guess you like

Origin blog.csdn.net/iSunwish/article/details/87858960