当前位置:网站首页>【QT】定时器
【QT】定时器
2022-07-04 04:24:00 【StudyWinter】
1 第一种方式
(1)利用事件 void timerEvent ( QTimerEvent * ev)
(2)启动定时器 startTimer( 1000) 毫秒单位
(3)timerEvent 的返回值是定时器的唯一标示 可以和ev->timerid 做比较
需求:设置两个定时器,时间不一样。
(1)首先是重写timerEvent函数
这里加上UI的label控件
重写
// 定时器事件
void Widget::timerEvent(QTimerEvent *event) {
if (event->timerId() == this->m_Id1) {
static int num = 1;
ui->label->setText(QString::number(num++));
} else if (event->timerId() == this->m_Id2){
static int num2 = 1;
ui->label_2->setText(QString::number(num2++));
}
}
(2)因为是两个定时器,在类中加入成员属性,各表示两个定时器的返回值
(3)启动两个定时器
// 启动定时器
this->m_Id1 = startTimer(1000); // 毫秒级
this->m_Id2 = startTimer(2000);
(4)效果
2 第二种方式
定时器类
// 定时器类
QTimer* timer = new QTimer(this);
timer->start(500); // 单位是毫秒
// 监听定时器对象的信号
connect(timer, &QTimer::timeout, this, [=](){
static int num3 = 1;
ui->label_3->setText(QString::number(num3++));
});
效果
扩充,点击暂停按钮,定时器暂停
添加QPushbutton按钮,使用connect做连接
// 点击暂停,实现停止
connect(ui->btn_pause, &QPushButton::clicked, this, [=](){
timer->stop();
});
效果
边栏推荐
- Kivy tutorial 07 component and attribute binding implementation button button click to modify the label component (tutorial includes source code)
- 中科磐云—D模块解析以及评分标准
- Encryption and decryption
- Dp83848+ network cable hot plug
- Unity中RampTex介绍和应用: 溶解特效优化
- 【MATLAB】MATLAB 仿真 — 模拟调制系统 之 AM 调制过程
- Annex VI: defense work briefing docx
- 全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
- 定制一个自己项目里需要的分页器
- MySQL JDBC programming
猜你喜欢
How do good test / development programmers practice? Where to go
Detailed comparison of Hynix emmc5.0 and 5.1 series
rac删除损坏的磁盘组
Create ASM disk through DD
Notes on the paper "cross view transformers for real time map view semantic segmentation"
Correct the classpath of your application so that it contains a single, compatible version of com.go
中科磐云—模块A 基础设施设置与安全加固 评分标准
关于solidworks standard无法获得许可 8544问题的总结
DCDC电源电流定义
Beipiao programmer, 20K monthly salary, 15W a year, normal?
随机推荐
附件二:攻防演练保密协议.docx
【MATLAB】MATLAB 仿真数字带通传输系统 — QPSK 和 OQPSK 系统
Drozer tool
What is context?
[wechat applet] good looking carousel map component
抓包整理外篇fiddler———— 会话栏与过滤器
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
C language one-way linked list exercise
【MATLAB】MATLAB 仿真模拟调制系统 — DSB 系统
RAC delete damaged disk group
Utiliser des unités de mesure dans votre code pour une vie meilleure
1. Mx6u-alpha development board (LED drive experiment in C language version)
定制一个自己项目里需要的分页器
我们认为消费互联网发展到最后,依然会局限于互联网行业本身
令人头痛的延时双删
【MATLAB】通信信号调制通用函数 — 带通滤波器
Sécurité du réseau dans les écoles professionnelles secondaires - preuve de mémoire
Useful plug-ins for vscode
红队视角下的防御体系突破之第二篇案例分析
LeetCode136+128+152+148