qt5--定时器
定时器方式一:----定时器事件
需要 #include <QTimerEvent>
#include "win.h" #include <QDebug> #include <QPushButton> Win::Win(QWidget *parent) : QWidget(parent) { this->resize(500,400); this->setWindowTitle("定时器"); this->move(700,100); QPushButton* btn=new QPushButton("按钮",this); btn->move(400,350); connect(btn,&QPushButton::clicked,this,&Win::A); label=new QLabel("标签标签",this); label->move(10,10); label->resize(200,50); label->setFrameShape(QFrame::Box); label1=new QLabel("标签1",this); label1->move(10,70); ID=startTimer(1000);//启动定时器事件,创建一个定时器并返回定时器ID //参数:单位毫秒---每隔n毫秒时间,就执行一次定时器事件 //返回值:定时器ID号 ID1=startTimer(2000); } void Win::timerEvent(QTimerEvent *e){ static int i,j=0; if(e->timerId()==ID){ //如果定时号是ID label->setText(QString::number(i++)); } if(e->timerId()==ID1){ label1->setText(QString::number(j++)); } } Win::~Win() { } void Win::A(){ }
void killTimer(int id); //停止 ID 为 id 的计时器,ID 由 startTimer()函数返回
定时器方式二:----QTimer类
需要 #include <QTimer>
方法一 ----推荐
win.h
#ifndef WIN_H #define WIN_H #include <QWidget> #include <QLabel> #include <QPushButton> #include <QTimer> //导入定时器头文件 #include <QDebug> #include <ratio> #include <chrono> class Win : public QWidget { Q_OBJECT public: Win(QWidget *parent = nullptr); ~Win(); private: QLabel* label; QPushButton* button; QPushButton* button1; QPushButton* button2; QTimer* timer1; //创建定时器 public slots: void f(); void kaisi(); void tinzi(); void qita(); }; #endif // WIN_H
win.cpp
#include "win.h" Win::Win(QWidget *parent) : QWidget(parent) { this->resize(250,130); label=new QLabel("0",this); button=new QPushButton("开始",this); button1=new QPushButton("停止",this); button2=new QPushButton("其它",this); label->move(100,10); label->resize(50,50); button->move(30,70); button1->move(130,70); button2->move(80,100); connect(button,SIGNAL(clicked(void)),this,SLOT(kaisi())); connect(button1,SIGNAL(clicked(void)),this,SLOT(tinzi())); connect(button2,SIGNAL(clicked(void)),this,SLOT(qita())); timer1=new QTimer(this); connect(timer1, SIGNAL(timeout()), this, SLOT(f())); //把 timeout 信号关联到槽f } Win::~Win() { } void Win::f() { static int i=0; label->setText(QString::number(i++)); } void Win::kaisi() { timer1->start(500); //启动定时器 //参数:每个n毫秒发送信号(timeout),单位:毫秒 //若计时器正在运行,则它将被停止并重新启动 } void Win::tinzi() { timer1->stop();//定时器停止 } void Win::qita() { bool b=timer1->isActive(); //若计时器正在运行,则返回 true,否则返回 false //timer1->setInterval(std::chrono::milliseconds(2000)); //修改定时器的时间间隔 //chrono 是 C++11 的标准库 int n=timer1->interval();//返回定时器的时间间隔 //单位:毫秒 n=timer1->remainingTime(); //获取以毫秒为单位的计时器的剩余时间。 //返回发出timeout信号的剩余时间 //若计时器过期,则返回 0,若计时器处于非活动状态(停止状态),则返回−1 timer1->setTimerType(Qt::PreciseTimer); //设置计时器的精度 /* Qt::PreciseTimer 0 精确的计时器,试图保持毫秒的精度 Qt::CoarseTimer 1 粗略的计时器,试图保持精度在期望间隔的 5%以内。 Qt::VeryCoarseTimer 2 非常粗略的计时器,只保持完整的秒的精度 */ Qt::TimerType x=timer1->timerType(); //返回计时器的精度 //Qt::PreciseTimer n=timer1->timerId();//若计时器正在运行,则返回其 ID,否则返回−1 qDebug()<<n; }
上面工程下载:链接:https://pan.baidu.com/s/1h0PlIcvz6DgfcBK6AiHaBg 提取码:6666
方法二--singleShot信号
condition = true;//控制singleShot信号 if(condition == true){ QTimer::singleShot(1000, this, SLOT(f())); //1000毫秒之后调用槽 f(注意:只会调用一次) /* 参数1:间隔时间--单位毫秒 参数2:信号接受者 参数3:槽函数 */ }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)