qt定时器类QTimer

QTimer类是Qt库中的定时器组件,用于周期性和一次性触发时间间隔。创建QTimer对象并调用start()函数来启动定时器,当超时会发出timeout()信号。可以通过设置setInterval()改变时间间隔,setSingleShot()控制是否只触发一次。示例代码展示了如何创建周期性和一次性定时器,并通过信号槽机制响应timeout()信号更新UI。
摘要由CSDN通过智能技术生成

使用方式:创建一个QTimer类对象,然后调用其 start() 函数开启定时器,此后QTimer对象就会周期性的发出 timeout() 信号。

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);

信号

这个类的信号只有一个, 当定时器超时时,该信号就会被发射出来。给这个信号通过conect()关联一个槽函数, 就可以在槽函数中处理超时事件了。

[signal] void QTimer::timeout();

// 启动或重新启动定时器,超时间隔为msec毫秒
[slot] void QTimer::start(int msec);
//如果不指定,可以调用:
void QTimer::setInterval(int msec);

当时间超时,就会发射QTimer::timeout();信号。

例子

周期性定时器

#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;
}


在这里插入图片描述
在这里插入图片描述
每过一秒就触发一次timeout信号,从而更新时间。。
在这里插入图片描述

一次性定时器
点击一次,当时间超时,只发送一次timeout信号

    //一次性定时器
    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);
    });

在这里插入图片描述
在这里插入图片描述
点击一次,时间超时,触发超时信号,槽函数显示当前时间(由于超时信号只发送一次,时间变化一次后,之后就不变化了)
再点击一次:
在这里插入图片描述
又更新一次。

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);
        });
    });

在这里插入图片描述
在这里插入图片描述
点一次,时间到了就触发一次。
再点一次
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值