定时器方式一:----定时器事件
需要 #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(){
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
void killTimer(int id); //停止 ID 为 id 的计时器,ID 由 startTimer()函数返回
定时器方式二:----QTimer类
需要 #include <QTimer>
方法一 ----推荐
win.h
#ifndef WIN_H
#define WIN_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QTimer> //导入定时器头文件
#include <QDebug>
#include <ratio>
#include <chrono>
class Win : public QWidget
{
Q_OBJECT
public:
Win(QWidget *parent = nullptr);
~Win();
private:
QLabel* label;
QPushButton* button;
QPushButton* button1;
QPushButton* button2;
QTimer* timer1; //创建定时器
public slots:
void f();
void kaisi();
void tinzi();
void qita();
};
#endif // WIN_H
win.cpp
#include "win.h"
Win::Win(QWidget *parent)
: QWidget(parent)
{
this->resize(250,130);
label=new QLabel("0",this);
button=new QPushButton("开始",this);
button1=new QPushButton("停止",this);
button2=new QPushButton("其它",this);
label->move(100,10);
label->resize(50,50);
button->move(30,70);
button1->move(130,70);
button2->move(80,100);
connect(button,SIGNAL(clicked(void)),this,SLOT(kaisi()));
connect(button1,SIGNAL(clicked(void)),this,SLOT(tinzi()));
connect(button2,SIGNAL(clicked(void)),this,SLOT(qita()));
timer1=new QTimer(this);
connect(timer1, SIGNAL(timeout()), this, SLOT(f())); //把 timeout 信号关联到槽f
}
Win::~Win()
{
}
void Win::f()
{
static int i=0;
label->setText(QString::number(i++));
}
void Win::kaisi()
{
timer1->start(500); //启动定时器
//参数:每个n毫秒发送信号(timeout),单位:毫秒
//若计时器正在运行,则它将被停止并重新启动
}
void Win::tinzi()
{
timer1->stop();//定时器停止
}
void Win::qita()
{
bool b=timer1->isActive(); //若计时器正在运行,则返回 true,否则返回 false
//timer1->setInterval(std::chrono::milliseconds(2000)); //修改定时器的时间间隔
//chrono 是 C++11 的标准库
int n=timer1->interval();//返回定时器的时间间隔
//单位:毫秒
n=timer1->remainingTime(); //获取以毫秒为单位的计时器的剩余时间。
//返回发出timeout信号的剩余时间
//若计时器过期,则返回 0,若计时器处于非活动状态(停止状态),则返回−1
timer1->setTimerType(Qt::PreciseTimer); //设置计时器的精度
/*
Qt::PreciseTimer 0 精确的计时器,试图保持毫秒的精度
Qt::CoarseTimer 1 粗略的计时器,试图保持精度在期望间隔的 5%以内。
Qt::VeryCoarseTimer 2 非常粗略的计时器,只保持完整的秒的精度
*/
Qt::TimerType x=timer1->timerType(); //返回计时器的精度
//Qt::PreciseTimer
n=timer1->timerId();//若计时器正在运行,则返回其 ID,否则返回−1
qDebug()<<n;
}
方法二--singleShot信号
condition = true;//控制singleShot信号
if(condition == true){
QTimer::singleShot(1000, this, SLOT(f())); //1000毫秒之后调用槽 f(注意:只会调用一次)
/*
参数1:间隔时间--单位毫秒
参数2:信号接受者
参数3:槽函数
*/
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓