• QT中怎么设置定时器/周期任务/定时触发任务


    Qt中定时器的使用有两种方法,一种是使用QObject类提供的定时器,还有一种就是使用QTimer类。

    其精确度一般依赖于操作系统和硬件,但一般支持20ms。下面将分别介绍两种方法来使用定时器。

    QObject类提供的定时器

    QObject中的定时器的使用,需要用到三个函数

    1、 int QObject::startTimer ( int interval ) ;

    ​ 这个是开启一个定时器的函数,他的参数interval是毫秒级别。当开启成功后会返回这个定时器的ID, 并且每隔interval 时间后会进入timerEvent 函数。直到定时器被杀死。

    2、 void QObject::timerEvent ( QTimerEvent * event );

    当定时器超时后,会进入该事件timerEvent函数,需要重写timerEvent函数,在函数中通过判断event->timerId()来确定定时器,然后执行某个定时器的超时函数。

    3、 void QObject::killTimer ( int id );

    ​ 通过从startTimer返回的ID传入killTimer 函数中杀死定时器,结束定时器进入超时处理。

    以下是QObject中的定时器具体使用简单例子:

    先新建一个控制台项目

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-abaXM1UX-1670160150577)(/Users/apple/Library/Application Support/typora-user-images/image-20221204210606147.png)]

    项目目录结构如下图所示

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lP9yR5ha-1670160105452)(/Users/apple/Library/Application Support/typora-user-images/image-20221204210647875.png)]

    其中main.cpp

    #include 
    #include "mytimer.h"”
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MyTimer myTimer;
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    mytimer.h

    #ifndef MYTIMER_H
    #define MYTIMER_H
    #include 
    
    class MyTimer : public QObject
    {
        Q_OBJECT
    
    public:
        MyTimer(QObject* parent = NULL);
        ~MyTimer();
        void  handleTimeout();  //超时处理函数
        virtual void timerEvent( QTimerEvent *event);
    private:
        int m_nTimerID;
    };
    #endif // MYTIMER_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    mytimer.cpp

    #include "mytimer.h"
    
    #include
    #include 
    
    #define TIMER_TIMEOUT	(5*1000)
    
    MyTimer::MyTimer(QObject *parent)
        :QObject(parent)
    {
        m_nTimerID = this->startTimer(TIMER_TIMEOUT);
    }
    
    MyTimer::~MyTimer()
    {
    
    }
    
    void MyTimer::timerEvent(QTimerEvent *event)
    {
        if(event->timerId() == m_nTimerID){
            handleTimeout();
        }
    }
    
    void MyTimer::handleTimeout()
    {
        qDebug()<<"Enter timeout processing function\n";
        //killTimer(m_nTimerID);//注意这句话,如果不注释掉,则只会执行一次
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    运行结果如下图所示

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-otbFFUio-1670160105453)(/Users/apple/Library/Application Support/typora-user-images/image-20221204210958810.png)]

    QTimer类提供的定时器

    1、 首先创建一个定时器类的对象

    QTimer *timer = new QTimer(this);

    2、 timer 超时后会发出timeout()信号,所以在创建好定时器对象后给其建立信号与槽

    connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));

    3、 在需要开启定时器的地方调用void QTimer::start ( int msec );

    这个start函数参数也是毫秒级别;

    timer->start(msec );

    4、 在自己的超时槽函数里面做超时处理。

    以下是QTimer定时器类具体使用简单例子:

    和上面一样,新建一个控制台应用项目。

    项目目录结构如下图所示

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OrrCkwtS-1670160105457)(/Users/apple/Library/Application Support/typora-user-images/image-20221204211809589.png)]

    其中main.cpp

    #include 
    #include "mytimer.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        MyTimer mytimer;
    
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    mytimer.h

    #ifndef _MYTIMER_H
    #define _MYTIMER_H
    
    #include 
    class QTimer;
    class MyTimer : public QObject
    {
        Q_OBJECT
    
    public:
        MyTimer(QObject* parent = NULL);
        ~MyTimer();
    public slots:
        void handleTimeout();  //超时处理函数
    private:
        QTimer *m_pTimer;
    };
    
    #endif //_MYTIMER_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    mytimer.cpp

    #include "mytimer.h"
    
    #include
    #include 
    
    #define TIMER_TIMEOUT	(5*1000)
    
    MyTimer::MyTimer(QObject *parent)
        :QObject(parent)
    {
        m_pTimer = new QTimer(this);
        connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
        m_pTimer->start(TIMER_TIMEOUT);
    }
    
    MyTimer::~MyTimer()
    {
    
    }
    
    void MyTimer::handleTimeout()
    {
        qDebug()<<"Enter timeout processing function\n";
    //    if(m_pTimer->isActive()){//不注释这几句,超时处理函数只会处理一次
    //        m_pTimer->stop();
    //    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    运行结果如下

    请添加图片描述

  • 相关阅读:
    IO子系统(一) — 块设备驱动程序
    Actipro Windows Forms Controls 22.1.3 注册版
    CiscoCUCM电话注册
    【学习QT必备的C++基础】C++继承、派生与多态
    指静脉处理综合代码(orz)
    使用 Spacesniffer 找回 48G 系统存储空间的总结
    c++成员变量和函数的储存
    JVS-rules中的基础与复合变量:规则引擎的心脏
    Python高级语法-装饰器(Python语法糖)
    [算法][数组][leetcode]2391. 收集垃圾的最少总时间
  • 原文地址:https://blog.csdn.net/qq_41854911/article/details/128177740