当前位置:网站首页>【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();
});效果

边栏推荐
- Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
- 【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
- Share some of my telecommuting experience
- 练习-冒泡排序
- Annex I: power of attorney for 202x XXX attack and defense drill
- 通过dd创建asm disk
- 【无标题】
- 【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(第 I 类部分响应波形)的眼图
- Correct the classpath of your application so that it contains a single, compatible version of com.go
- Simple g++ and GDB debugging
猜你喜欢
随机推荐
Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
PostgreSQL 正式超越 MySQL,这家伙也太强了吧!
QT qtableview data column width adaptation
红队视角下的防御体系突破之第一篇介绍、阶段、方法
DCDC电源电流定义
海力士EMMC5.0及5.1系列对比详解
Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises
YoloV6实战:手把手教你使用Yolov6进行物体检测(附数据集)
First knowledge of batch processing
在代碼中使用度量單比特,從而生活更美好
【MATLAB】通信信号调制通用函数 — 带通滤波器
中科磐云—D模块解析以及评分标准
[go] database framework Gorm
Flutter 调用高德地图APP实现位置搜索、路线规划、逆地理编码
【Go】数据库框架gorm
Simple g++ and GDB debugging
Annex 2-2 confidentiality commitment docx
GUI application: socket network chat room
《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记









