QT中定时器的使用
发布日期:2021-06-30 10:28:32
浏览次数:2
分类:技术文章
本文共 1624 字,大约阅读时间需要 5 分钟。
方法一
使用 t i m e r E v e n t timerEvent timerEvent
[override virtual protected] void QTimer::timerEvent(QTimerEvent *e)
所以只需要在 W i d g e t . h Widget.h Widget.h里声明一下,然后去 W i d g e t . c p p Widget.cpp Widget.cpp实现即可
具体的操作写在函数 t i m e E v e n t timeEvent timeEvent中
但是怎么触发时间呢??我们需要激活一下,也就是
s t a r t T i m e r ( x ) startTimer(x) startTimer(x)
其中 x x x表示间隔 x m s x\ ms x ms触发一次
然后 s t a r t T i m e r startTimer startTimer会返回一个 i n t int int类型的数,根据这个可以判断当前是哪个定时器
比如下面,实现了在 L a b e l Label Label中不停增长数字
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget){ ui->setupUi(this); id1 = startTimer(100); id2 = startTimer(1000);}void Widget::timerEvent(QTimerEvent *e){ if( e->timerId()==id1 ) { static int num1 = 1; ui->label_1->setText(QString::number(num1++)); } if( e->timerId()==id2 ) { static int num2 = 1; ui->label_2->setText(QString::number(num2++)); }}Widget::~Widget(){ delete ui;}
方法二
使用类 Q T i m e r QTimer QTimer实例对象,靠发射信号来写
ui->setupUi(this); QTimer *timer1 = new QTimer(this); timer1->start(100); connect(timer1,&QTimer::timeout, [=]() { ui->label_1->setText(QString::number(id1++)); } ); QTimer *timer2 = new QTimer(this); timer2->start(1000); connect(timer2,&QTimer::timeout, [=]() { ui->label_2->setText(QString::number(id2++)); } );
逻辑更加清晰
现在继续设置一个按钮,按下就停止计数
其实还是使用信号来写,用类 s t o p stop stop一下就好了
connect(ui->Btn1,&QPushButton::clicked,[=]() { timer2->stop(); } );
转载地址:https://issue-is-vegetable.blog.csdn.net/article/details/113663182 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月06日 00时14分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
UVA-12555 - Baby Me(控制精度)
2019-04-30
Java大数的使用
2019-04-30
Java Number类
2019-04-30
半数集与半数单集问题
2019-04-30
HDU-1297 Children’s Queue(递推)(高精度)
2019-04-30
HDU-5601 N*M bulbs(推导||规律)
2019-04-30
HDU-1316 How Many Fibs?(Java大数)
2019-04-30
UVA-10579 Fibonacci Numbers(Java大数)
2019-04-30
HDU-2680 Choose the best route(SPFA)
2019-04-30
codeforces 600 A. Extract Numbers(字符串模拟)
2019-04-30
HDU-5878 I Count Two Three(打表)
2019-04-30
HDU-5879 Cure(精度)(极限)
2019-04-30
UVA 1395 Slim Span(MST)
2019-04-30
HDU-5912 Fraction(迭代)
2019-04-30
HDU-5918 Sequence I(暴力)(KMP)
2019-04-30
NYOJ-128 前缀式计算(栈)
2019-04-30