Qt provides a timer class QTimer, which needs to include a header file when used
#include <QTimer>
Introduction to QTimer class methods:
- void start(int msec); Start timer, msec milliseconds of timing interval
- void stop(); End timing
QTimer signal:
- void timeout(QPrivateSignal); This signal needs to be bound when linking the timer
Basic use of QTimer
The following describes how to use Qt timer to realize a stopwatch.
Create a Qt MainWindow project. The interface is as follows:
LCD control QLCDNumber for time display
Operation effect
MainWindow header file code:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimer> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_btnStart_clicked(); void on_btnStop_clicked(); void on_btnContinue_clicked(); void on_timer(); private: Ui::MainWindow *ui; QTimer *m_pTimer; int m_counts = 0; //Time count }; #endif // MAINWINDOW_H
The timer object pointer m is defined in the header file_ Ptimer, millisecond count m_counts
MainWindow cpp code
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //For LCD control, you need to set the number of displayed numbers first, and then display numbers, otherwise the display is incomplete ui->lcdNumber->setDigitCount(9); ui->lcdNumber->display("00:00:000"); //new QTimer to set the timer type m_pTimer = new QTimer(this); m_pTimer->setTimerType(Qt::PreciseTimer); //Link timer signal timeout. When start is called, the slot function on is triggered_ timer connect(m_pTimer, &QTimer::timeout, this, &MainWindow::on_timer); } MainWindow::~MainWindow() { delete ui; } //Start timing void MainWindow::on_btnStart_clicked() { m_pTimer->start(1); //Start the timer with a frequency of 1ms m_counts = 0; } //Pause timing void MainWindow::on_btnStop_clicked() { m_pTimer->stop(); } //Continue timing void MainWindow::on_btnContinue_clicked() { m_pTimer->start(1); } void MainWindow::on_timer() { m_counts++; char buf[64]; if(m_counts < 1000) //Within 1 second { sprintf(buf, "00:00:%03d", m_counts); } else if(m_counts > 1000 && m_counts < 60 * 1000) //Within one minute { int seconds = m_counts / 1000; int ms = m_counts - seconds * 1000; sprintf(buf, "00:%02d:%03d", seconds, ms); } else { int mins = m_counts / (60 * 1000); int seconds = (m_counts - mins * 60 * 1000) / 1000; int ms = m_counts - mins * 60 * 1000 - seconds * 1000; sprintf(buf, "%02d:%02d:%03d", mins, seconds, ms); } ui->lcdNumber->display(buf); }
It should be noted that QTimer can set the type, that is, call setTimerType to set it. The timer types declared by QTimer are as follows:
enum TimerType { PreciseTimer, CoarseTimer, VeryCoarseTimer };
What do these three mean? Qt assistant explains as follows:
- Qt::PreciseTimer a precise timer attempts to maintain the accuracy of milliseconds
- Qt::CoarseTimer the coarse timer attempts to keep the accuracy within 5% of the required interval
- Qt::VeryCoarseTimer very rough timers can only maintain full second accuracy
When new QTimer is selected, the timer accuracy can be set as required.
On UNIX (including Linux, macOS and iOS), Qt will maintain the precision of Qt::PreciseTimer in milliseconds. For Qt::CoarseTimer, the interval will be adjusted to 5% to align the timer with other timers expected to trigger at or about the same time. The goal is to wake up most timers at the same time, thereby reducing CPU wake-up and power consumption.
On Windows, Qt will use the Windows multimedia timer tool (if available) for Qt::PreciseTimer and the normal Windows timer for Qt::CoarseTimer and Qt::VeryCoarseTimer.
On all platforms, the interval of Qt::VeryCoarseTimer is rounded to the nearest full second (for example, the interval of 23500ms will be rounded to 24000ms and 20300ms will be rounded to 20000ms).
QTimer::singleShot instructions
In addition to performing some kind of operation periodically, QTimer can perform a single operation. The static function singleShot of QTimer is to achieve such a single operation function. SingleShot calls a slot after a given time interval msec (millisecond), for example, the following code:
void MainWindow::on_pushButton_clicked() { QTimer::singleShot(3000,this,[=]{ //Code you want to execute qDebug() << "hello world" ; }); }
After 3000ms, hello world will be printed. The lambdab function used above can also be written with class member function. The code is as follows:
The header file defines the member function do in the header file_ Single, slot function for QTimer::singleShot
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); //Define a member function and make a slot function for QTimer::singleShot void do_single(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; };
cpp file
void MainWindow::on_pushButton_clicked() { // QTimer::singleShot(3000,this,[=]{ // //Code you want to execute // qDebug() << "hello world" ; // }); QTimer::singleShot(3000, this, &MainWindow::do_single); } void MainWindow::do_single() { qDebug() << "hello world" ; }
Note that singleShot will only trigger once, send a signal once, and then execute the slot function.