当前位置:网站首页>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

边栏推荐
猜你喜欢

The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j

Keep alive cache component in Vue

Tiktok practice ~ sharing module ~ generate short video QR code

Unit test of boot

MySQL recharge
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

vue中缓存组件keep-alive

清华大学就光刻机发声,ASML立马加紧向中国出口光刻机

慕课11、微服务的用户认证与授权

【推荐收藏】这8个常用缺失值填充技巧一定要掌握
随机推荐
孙老师版本JDBC(2022年6月12日21:34:25)
Serial port application program based on gd32
Unity——Mathf. Similarities and differences between atan and atan2
WebView load pdf
[recommended collection] these 8 common missing value filling skills must be mastered
数据库SQL语句撰写
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
【贝叶斯分类2】朴素贝叶斯分类器
Tiktok practice ~ sharing module ~ generate short video QR code
Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
MySQL recharge
c语言99乘法表
Is it safe to open a securities account? Is there any danger
Gee: calculate the maximum and minimum values of pixels in the image area
C# 练习。类列表加记录,显示记录和清空记录
What are the specific steps for opening a stock account? Is it safe to open an account online?
Tiktok practice ~ search page ~ video details
网上办理中金财富开户安全吗?
windows系统下怎么安装mysql8.0数据库?(图文教程)
定长内存池