当前位置:网站首页>【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();
});
效果
边栏推荐
猜你喜欢
Flutter ‘/usr/lib/libswiftCore.dylib‘ (no such file)
20000 words will take you to master multithreading
Formatted text of Kivy tutorial (tutorial includes source code)
关于solidworks standard无法获得许可 8544问题的总结
Yolov6 practice: teach you to use yolov6 for object detection (with data set)
Developing mqtt access program under QT
附件六:防守工作简报.docx
令人头痛的延时双删
附件六:防守工作簡報.docx
Correct the classpath of your application so that it contains a single, compatible version of com.go
随机推荐
6-5漏洞利用-SSH弱口令破解利用
红队视角下的防御体系突破之第一篇介绍、阶段、方法
When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
Kivy tutorial 07 component and attribute binding implementation button button click to modify the label component (tutorial includes source code)
中科磐云—D模块解析以及评分标准
Zhongke Panyun - 2022 Guangxi reverse analysis ideas
appliedzkp的zkevm(12)State Proof
cmake
Network - vxlan
RPC Technology
附件三:防守方评分标准.docx
Definition of DCDC power supply current
Beipiao programmer, 20K monthly salary, 15W a year, normal?
Kivy tutorial custom fonts (tutorial with source code)
附件二:攻防演练保密协议.docx
Change the background color of Kivy tutorial (tutorial includes source code)
【无标题】
Detailed comparison of Hynix emmc5.0 and 5.1 series
CRS-4013: This command is not supported in a single-node configuration.
中科磐云—2022广东木马信息获取解析