设为首页收藏本站 |天气与日历| 2025-07-01 星期二 22:36:00 (建党节) 乙巳(蛇)年 六月初七 亥时
     
切换到窄版

私人站点

 找回密码
 立即注册
搜索
查看: 204|回复: 0

定时器timer

[复制链接]

954

主题

954

帖子

3879

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3879
发表于 2022-2-7 21:47:12 | 显示全部楼层 |阅读模式
1 定时器
1.1 定时器事件   void timerEvent( QTimerEvent * e)
1.2 启动定时器  id1 = startTimer(毫秒)
1.3 判断具体定时器标准  e->timerId() == id1

[C++] 纯文本查看 复制代码
    //开启定时器
    id1 = startTimer(1000); //单位 毫秒


    id2 = startTimer(2000); //单位 毫秒


//添加定时器事件
void Widget::timerEvent(QTimerEvent * e)
{

    if(e->timerId() == id1)
    {
        //每隔1秒中 让label_2数字++
        static int num = 1;
        ui->label_2->setText( QString::number(num++) );
    }
    if(e->timerId() == id2)
    {
        //每隔2秒让label_3数字++
        static int num2 = 1;
        ui->label_3->setText( QString::number(num2++) );
    }
}





通过定时器类实现  (推荐)
2.2 创建定时器对象
2.2.1 QTimer * timer1 = new QTimer(this);
2.3 开启定时器
2.3.1 timer1->start(x毫秒)
2.4 每隔x 会抛出一个信号timeout出来

2.5 监听信号处理逻辑
2.6 暂停定时器  timer1->stop();

[C++] 纯文本查看 复制代码
  QTimer * timer1 = new QTimer(this);
    timer1->start(500);

    //每隔0.5秒 会抛出一个信号timeout出来
    connect(timer1,&QTimer::timeout , [=](){
        //每隔0.5秒中 让label_4数字++
        static int num = 1;
        ui->label_4->setText( QString::number(num++) );
    });


    //点击暂停按钮  停止定时器
    connect(ui->pushButton,&QPushButton::clicked,[=](){
         timer1->stop();

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|编程站点 ( 冀ICP备2023028127号-2 )|友链申请|

GMT+8, 2025-7-1 22:36 , Processed in 0.088013 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表