当前位置:网站首页>Two methods of QT to realize timer
Two methods of QT to realize timer
2022-06-26 20:38:00 【Little ash of the prairie】
Method 1
1、 Rewrite the virtual function
void timerEvent(QTimerEvent* e);
2、 Start timer
- Return value is timer id, The parameter is timing interval , The unit is millisecond
int startTimer(int interval)
Code
- 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;
}
// Timed interval to , The function will be called automatically
void Widget::timerEvent(QTimerEvent *e)
{
// Conduct relevant business
if(e->timerId() == timerId){
static int i = 0;
ui->lineEdit->setText(QString("timer: %1").arg(i++));
}
}
void Widget::on_pushButton_clicked()
{
// Start timer . Parameters : Timing time (ms)
timerId = startTimer(1000);
}
void Widget::on_pushButton_close_clicked()
{
killTimer(timerId);
}
Method 2
1、 Define a QTimer object
QTimer* timer;
2、 Start timer
- The parameter is timing interval , The unit is millisecond
void QTimer::start(std::chrono::milliseconds msec)
3、 Connect the signal slot
- Timed interval to ,QTimer Object emits a timeout The signal . Connect the signal slot , Perform relevant operations in the slot function
[signal] void QTimer::timeout()
Code
- 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 the signal slot
connect(timer, &QTimer::timeout, this, &Widget::timeoutSlot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
// Start timer
timer->start(1000);
}
void Widget::on_pushButton_close_clicked()
{
// off timer
timer->stop();
}
void Widget::timeoutSlot()
{
// Conduct relevant business
static int i = 0;
ui->lineEdit->setText(QString("timer: %1").arg(i++);
}
effect

边栏推荐
- Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
- C primer plus学习笔记 —— 3、字符的IO(输入/输出)
- Gd32 USB composite device file descriptor
- windows系统下怎么安装mysql8.0数据库?(图文教程)
- Is it safe to open an account for CICC Wealth Online?
- Tiktok practice ~ sharing module ~ generate short video QR code
- 股票开户的具体步骤是什么?网上开户安全吗?
- 【贝叶斯分类4】贝叶斯网
- [most detailed] latest and complete redis interview (42 tracks)
- C language 99 multiplication table
猜你喜欢

西瓜书重温(七): 贝叶斯分类器(手推+代码demo)

关于Qt数据库开发的一些冷知识

Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005

Review of watermelon book (VII): Bayesian classifier (manual push + code demo)

Kubernetes 资源拓扑感知调度优化

Tiktok practice ~ sharing module ~ copy short video link

Minimum spanning tree, shortest path, topology sorting, critical path

抖音实战~首页视频~下拉刷新

【贝叶斯分类3】半朴素贝叶斯分类器

Some cold knowledge about QT database development
随机推荐
抖音实战~分享模块~生成短视频二维码
C exercise. Class list plus records, display records and clear records
Unity——Mathf. Similarities and differences between atan and atan2
Tiktok practice ~ homepage video ~ pull-down refresh
Keep alive cache component in Vue
慕课8、服务容错-Sentinel
710. random numbers in the blacklist
动态规划111
0 basic C language (0)
Dynamic planning 111
When are global variables initialized before entering the main function?
The two files are merged into a third file.
30. concatenate substrings of all words
MySQL中存储过程的详细详解
Stringutils judge whether the string is empty
SentinelResource注解詳解
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
开户可以在网上开么?能安全吗?
Detailed explanation of shutter textfield
浏览器事件循环