widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
//在widget中声明定时器事件,在widget.cpp中实现定时器函数
//声明定时器事件
virtual void timerEvent(QTimerEvent *e);
private:
Ui::Widget *ui;
//在widget.h中定义两个定时器id,存放各自的定时器id
int id1;
int id2;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//开启两个定时器
this->id1 = this->startTimer(1000);//1秒
this->id2 = this->startTimer(2000);
//创建一个定时器对象
QTimer *time = new QTimer(this);
//让定时器动起来
connect(time,&QTimer::timeout,[=](){
static int num=0;
ui->label_3->setText(QString::number(num++));
});
//启动定时器
connect(ui->pushButton,&QPushButton::clicked,[=](){
time->start(1000);
});
//暂停定时器
connect(ui->pushButton_2,&QPushButton::clicked,[=](){
time->stop();
});
//通过singleShot完成延时
QTimer::singleShot(5000,[=](){
ui->label_4->setText("哈哈,我喜欢你");
});
}
Widget::~Widget()
{
delete ui;
}
//实现定时器事件
void Widget::timerEvent(QTimerEvent *e)
{
static int num1 = 0;
static int num2 = 0;
//必须通过timerid判断哪个id触发
if(e->timerId() == this->id1)
{
ui->label->setText(QString::number(num1++));
}
else if(e->timerId() == this->id2)
{
ui->label_2->setText(QString::number(num2++));
}
}