当前位置:网站首页>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++);
}
效果

边栏推荐
- Tiktok practice ~ search page ~ video details
- Chain game development finished product source code chain game system development details
- C language 99 multiplication table
- c语言99乘法表
- Nftgamefi chain game system development detailed solution - chain game system development principle analysis
- 好物推荐:移动端开发安全工具
- Dynamic planning 111
- Keep alive cache component in Vue
- 0基础c语言(0)
- SentinelResource注解詳解
猜你喜欢

Idea error: process terminated

数据库SQL语句撰写

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered

GEE:计算image区域内像素最大最小值
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

Tiktok practice ~ homepage video ~ pull-down refresh

论数据库的传统与未来之争之溯源溯本----AWS系列专栏

mysql的充值问题

Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
随机推荐
Gd32 USB composite device file descriptor
Tiktok practice ~ sharing module ~ generate short video QR code
Tiktok practice ~ sharing module ~ short video download (save to photo album)
mysql的充值问题
Muke 8. Service fault tolerance Sentinel
mysql存储过程
String string is converted to jsonarray and parsed
Database SQL statement writing
Why don't I recommend going to sap training institution for training?
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
When are global variables initialized before entering the main function?
Six necessary threat tracking tools for threat hunters
MySQL - table creation and management
JS mobile terminal touch screen event
Idea error: process terminated
开户可以在网上开么?能安全吗?
Stringutils judge whether the string is empty
关于不等式取值转义的思路
抖音实战~分享模块~复制短视频链接
网上开户万一免五到底安不安全?