qt timer class QTimer

qt timer class QTimer

How to use: Create a QTimer class object, then call its start() function to start the timer, and then the QTimer object will periodically send out the timeout() signal.

API

// 构造函数
// 如果指定了父对象, 创建的堆内存可以自动析构
QTimer::QTimer(QObject *parent = nullptr);

// 设置定时器时间间隔为 msec 毫秒
// 默认值是0,一旦窗口系统事件队列中的所有事件都已经被处理完,一个时间间隔为0的QTimer就会触发
void QTimer::setInterval(int msec);
// 获取定时器的时间间隔, 返回值单位: 毫秒
int QTimer::interval() const;

// 根据指定的时间间隔启动或者重启定时器, 需要调用 setInterval() 设置时间间隔
[slot] void QTimer::start();
// 启动或重新启动定时器,超时间隔为msec毫秒。
[slot] void QTimer::start(int msec);
// 停止定时器。
[slot] void QTimer::stop();

// 设置定时器精度
/*
参数: 
    - Qt::PreciseTimer -> 精确的精度, 毫秒级
    - Qt::CoarseTimer  -> 粗糙的精度, 和1毫秒的误差在5%的范围内, 默认精度
    - Qt::VeryCoarseTimer -> 非常粗糙的精度, 精度在1秒左右
*/
void QTimer::setTimerType(Qt::TimerType atype);
Qt::TimerType QTimer::timerType() const;	// 获取当前定时器的精度

// 如果定时器正在运行,返回true; 否则返回false。
bool QTimer::isActive() const;

// 判断定时器是否只触发一次
bool QTimer::isSingleShot() const;
// 设置定时器是否只触发一次, 参数为true定时器只触发一次, 为false定时器重复触发, 默认为false
void QTimer::setSingleShot(bool singleShot);

Signal

There is only one signal of this class, which will be emitted when the timer expires. Associate this signal with a slot function through conect(), and you can handle the timeout event in the slot function.

[signal] void QTimer::timeout();

// Start or restart the timer, the timeout interval is msec milliseconds
[slot] void QTimer::start(int msec);
// If not specified, you can call:
void QTimer::setInterval(int msec);

When the time expires, QTimer::timeout();the signal is emitted.

example

periodic timer

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include "QTimer"
#include "QTime"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    tom = new me(this);
    myteacher = new teacher(this);
    QTimer* timer = new QTimer(this);
    timer->setTimerType(Qt::PreciseTimer);
    connect(ui->myPushButton1,&QPushButton::clicked,this,[=](){
    
    
        qDebug() <<"调用一次";
        tom->sendMsg();
    });
    connect(tom,&me::sendMsg,myteacher,&teacher::receiveMsg);
    //timer
    connect(ui->time,&QPushButton::clicked,this,[=](){
    
    
        //qDebug() <<"调用一次";
        if(timer->isActive()){
    
    
            timer->stop();
            ui->time->setText("点击开始计时");
        }else{
    
    
            timer->start(1000);
            ui->time->setText("点击关闭计时");
        }
    });
    //当定时间超时,调用槽函数
    connect(timer,&QTimer::timeout,this,[=](){
    
    
        QTime time = QTime::currentTime();
        QString timestr = time.toString("hh--mm--ss--zz");
        ui->time2->setText(timestr);

    });

}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}


insert image description here
insert image description here
The timeout signal is triggered every second to update the time. .
insert image description here

One-time timer
Click once, when the time expires, only send timeout signal once

    //一次性定时器
    QTimer* timerone = new QTimer(this);
    timerone->setTimerType(Qt::PreciseTimer);
    timerone->setSingleShot(true);//一次性
    connect(ui->timeonce,&QPushButton::clicked,this,[=](){
    
    
        qDebug() <<"调用一次";
        if(timerone->isActive()){
    
    
            timerone->stop();
            ui->timeonce->setText("点击开始计时");
        }else{
    
    
            timerone->start(1000);
            ui->timeonce->setText("点击关闭计时");
        }
    });
    //当定时间超时,调用槽函数
    connect(timerone,&QTimer::timeout,this,[=](){
    
    
        QTime time = QTime::currentTime();
        QString timestr = time.toString("hh--mm--ss--zz");
        ui->time2->setText(timestr);
    });

insert image description here
insert image description here
Click once, the time expires, the timeout signal is triggered, and the slot function displays the current time (since the timeout signal is only sent once, after the time changes once, it will not change afterwards). Click again: update
again
insert image description here
.

static public function

/*
功能: 在msec毫秒后发射一次信号, 并且只发射一次
参数:
	- msec:     在msec毫秒后发射信号
	- receiver: 接收信号的对象地址
	- method:   槽函数地址
*/
[static] void QTimer::singleShot(
        int msec, const QObject *receiver, 
        PointerToMemberFunction method);
    //一次性定时器 (使用[static] void QTimer::singleShot)
//    [static] void QTimer::singleShot(
//            int msec, const QObject *receiver,
//            PointerToMemberFunction method);
//     参数:
//	     - msec:     在msec毫秒后发射信号
//	     - receiver: 接收信号的对象地址
//	     - method:   槽函数地址
    connect(ui->timeonce2,&QPushButton::clicked,this,[=](){
    
    
        // 获取2s以后的系统时间, 不创建定时器对象, 直接使用类的静态方法
        QTimer::singleShot(2000,this,[=](){
    
    
            QTime tm = QTime::currentTime();
            // 格式化当前得到的系统时间
            QString tmstr = tm.toString("hh:mm:ss.zzz");
            // 设置要显示的时间
            ui->time2->setText(tmstr);
        });
    });

insert image description here
insert image description here
Click once, and it will trigger once when the time is up.
click again
insert image description here

Guess you like

Origin blog.csdn.net/qq_41701723/article/details/132016835