当前位置:网站首页>QT两种方法实现定时器
QT两种方法实现定时器
2022-06-26 20:20:00 【大草原的小灰灰】
方法一
1、重写虚函数
void timerEvent(QTimerEvent* e);
2、启动定时器
- 返回值为定时器id,参数为定时间隔,单位为毫秒
int startTimer(int interval)
代码
- widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void timerEvent(QTimerEvent *e);
private slots:
void on_pushButton_clicked();
void on_pushButton_close_clicked();
private:
Ui::Widget *ui;
int timerId;
};
#endif // WIDGET_H
- widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
// 定时间隔到,系统会自动调用该函数
void Widget::timerEvent(QTimerEvent *e)
{
//进行相关业务
if(e->timerId() == timerId){
static int i = 0;
ui->lineEdit->setText(QString("timer: %1").arg(i++));
}
}
void Widget::on_pushButton_clicked()
{
// 启动定时器。参数:定时时间(ms)
timerId = startTimer(1000);
}
void Widget::on_pushButton_close_clicked()
{
killTimer(timerId);
}
方法2
1、定义一个QTimer对象
QTimer* timer;
2、启动定时器
- 参数为定时间隔,单位为毫秒
void QTimer::start(std::chrono::milliseconds msec)
3、连接信号槽
- 定时间隔到,QTimer对象会发出一个timeout信号。连接信号槽,在槽函数中进行相关操作
[signal] void QTimer::timeout()
代码
- widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_close_clicked();
void timeoutSlot();
private:
Ui::Widget *ui;
QTimer* timer;
};
#endif // WIDGET_H
- widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
timer = new QTimer(this);
//连接信号槽
connect(timer, &QTimer::timeout, this, &Widget::timeoutSlot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
//启动定时器
timer->start(1000);
}
void Widget::on_pushButton_close_clicked()
{
//关闭定时器
timer->stop();
}
void Widget::timeoutSlot()
{
//进行相关业务
static int i = 0;
ui->lineEdit->setText(QString("timer: %1").arg(i++);
}
效果

边栏推荐
- When are global variables initialized before entering the main function?
- JWT操作工具类分享
- 710. random numbers in the blacklist
- 股票开户的具体步骤是什么?网上开户安全吗?
- Project practice 6: distributed transaction Seata
- Sentinelresource annotation details
- 关于Qt数据库开发的一些冷知识
- Tiktok practice ~ search page ~ scan QR code
- vue中缓存组件keep-alive
- 阿里云个人镜像仓库日常基本使用
猜你喜欢
随机推荐
Boot indicator monitoring
[most detailed] latest and complete redis interview (42 tracks)
Some cold knowledge about QT database development
【最详细】最新最全Redis面试大全(42道)
30. 串联所有单词的子串
Tiktok practice ~ sharing module ~ generate short video QR code
浏览器事件循环
mongoDB的三种基础备份方法
动态规划111
股票开户的具体步骤是什么?网上开户安全吗?
Daily basic use of alicloud personal image warehouse
8VC Venture Cup 2017 - Final Round C. Nikita and stack
孙老师版本JDBC(2022年6月12日21:34:25)
Project practice 6: distributed transaction Seata
Pinda general permission system (day 1~day 2)
Installation and use of logstash
超分之VRT
Tiktok practice ~ search page ~ scan QR code
Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
Is it safe to open an account for CICC Wealth Online?








