基础

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

// 根据指定时间间隔启动或者重启定时器, 需要调用 setInterval() 设置时间间隔
void QTimer::start();

// 启动或重新启动定时器超时间隔msec毫秒
void QTimer::start(int msec);

// 停止定时器
void QTimer::stop();

//当定时器超时时,该信号就会被发射出来。
[signal] void QTimer::timeout();

// 设置定时器时间间隔msec 毫秒默认值是0。
void QTimer::setInterval(int msec);

// 获取定时器时间间隔, 返回值单位: 毫秒
int QTimer::interval() const;

设置定时器精度

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

Qt::TimerType QTimer::timerType() const;	// 获取当前定时器的精度这里插入代码

其他API

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

// 判断定时是否触发一次
bool QTimer::isSingleShot() const;

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

小案列1

效果

在这里插入图片描述

思路
    //设置界面实时时间
    QTimer *timer = new QTimer(this);
    timer->start(1000);//启动定时
    connect(timer, &QTimer::timeout, [=](){
        QTime time = QTime::currentTime();//获取当前时间
        QString str = time.toString("hh:mm:ss");
        ui->label_curTime->setText(str);//显示到界面
    });

小案列2

使用QTimer类中全局静态 函数,实现延时

[static] void QTimer::singleShot(int msec, const QObject *receiver, PointerToMemberFunction method);
功能:msec 毫秒后发射一次信号, 并且只发射一次
参数:
	- msec:     在msec毫秒后发射信号
	- receiver: 接收信号对象地址
	- method:   槽函数地址
思路
//延时300毫秒切换窗口
QTimer::singleShot(300, this, [=](){// 延时300毫秒
    this->hide();//隐藏窗口
    chooseScene->show();//显示其他窗口
});

其他方法使用定时

返回定时器 Id
在这里插入图片描述
重写定时器事件利用 定时器 id 判断是哪一个定时器
在这里插入图片描述

详细教程可转

爱编程的大丙

原文地址:https://blog.csdn.net/CYS_2020/article/details/134700412

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_35562.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注