qt5--定时器

 

定时器方式一:----定时器事件

需要     #include <QTimerEvent>

 

#include "win.h"
#include <QDebug>
#include <QPushButton>


Win::Win(QWidget *parent)
    : QWidget(parent)
{
    this->resize(500,400);
    this->setWindowTitle("定时器");
    this->move(700,100);

    QPushButton* btn=new QPushButton("按钮",this);
    btn->move(400,350);
    connect(btn,&QPushButton::clicked,this,&Win::A);

    label=new QLabel("标签标签",this);
    label->move(10,10);
    label->resize(200,50);
    label->setFrameShape(QFrame::Box);

    label1=new QLabel("标签1",this);
    label1->move(10,70);

    ID=startTimer(1000);//启动定时器事件,创建一个定时器并返回定时器ID
    //参数:单位毫秒---每隔n毫秒时间,就执行一次定时器事件
    //返回值:定时器ID号
    ID1=startTimer(2000);


}
void Win::timerEvent(QTimerEvent *e){
    static int i,j=0;
    if(e->timerId()==ID){    //如果定时号是ID
        label->setText(QString::number(i++));
    }
    if(e->timerId()==ID1){
        label1->setText(QString::number(j++));
    }



}

Win::~Win()
{

}


void Win::A(){

}

 

定时器方式二:----QTimer类--推荐

需要   #include <QTimer>

 

#include "win.h"
#include <QDebug>
#include <QPushButton>


Win::Win(QWidget *parent)
    : QWidget(parent)
{
    this->resize(500,400);
    this->setWindowTitle("定时器");
    this->move(700,100);

    QPushButton* btn=new QPushButton("按钮",this);
    btn->move(400,350);
    connect(btn,&QPushButton::clicked,this,&Win::A);

    label=new QLabel("标签标签",this);
    label->move(10,10);
    label->resize(200,50);
    label->setFrameShape(QFrame::Box);

    label1=new QLabel("标签1",this);
    label1->move(10,70);

    timer1=new QTimer(this);  //创建定时器对象
    timer1->start(500);  //启动定时器
    //参数:每个n毫秒发送信号(timeout),单位:毫秒
    connect(timer1,&QTimer::timeout,[=](){
        static int i=0;
        label->setText(QString::number(i++));
    });  //信号连接函数
    //QTimer::timeout  时间到信号

    //timer1->stop();//定时器停止


}


Win::~Win()
{

}


void Win::A(){

}

 

 

 

 

转载于:https://www.cnblogs.com/liming19680104/p/11550712.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值