分享

Qt中定时器使用的两种方法...

 得思 2020-09-19

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

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

方法一: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中的定时器具体使用简单例子:

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

[cpp]  view plain  copy
  1. #include "mytimer.h"  
  2.   
  3. #include<QDebug>   
  4. #include <QTimerEvent>  
  5.   
  6. #define TIMER_TIMEOUT   (5*1000)  
  7.   
  8. MyTimer::MyTimer(QObject *parent)  
  9.     :QObject(parent)  
  10. {  
  11.     m_nTimerID = this->startTimer(TIMER_TIMEOUT);  
  12. }  
  13.   
  14. MyTimer::~MyTimer()  
  15. {  
  16.       
  17. }  
  18.   
  19. void MyTimer::timerEvent(QTimerEvent *event)  
  20. {  
  21.     if(event->timerId() == m_nTimerID){  
  22.         handleTimeout();  
  23.     }  
  24. }  
  25.   
  26. void MyTimer::handleTimeout()  
  27. {  
  28.     qDebug()<<"Enter timeout processing function\n";  
  29.     killTimer(m_nTimerID);  
  30. }  

方法二:使用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定时器类具体使用简单例子:

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

[cpp]  view plain  copy
  1. #include "mytimer.h"  
  2.   
  3. #include<QDebug>   
  4. #include <QTimer>  
  5.   
  6. #define TIMER_TIMEOUT   (5*1000)  
  7.   
  8. MyTimer::MyTimer(QObject *parent)  
  9.     :QObject(parent)  
  10. {  
  11.     m_pTimer = new QTimer(this);  
  12.     connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));  
  13.     m_pTimer->start(TIMER_TIMEOUT);  
  14. }  
  15.   
  16. MyTimer::~MyTimer()  
  17. {  
  18.       
  19. }  
  20.   
  21. void MyTimer::handleTimeout()  
  22. {  
  23.     qDebug()<<"Enter timeout processing function\n";  
  24.     if(m_pTimer->isActive()){  
  25.         m_pTimer->stop();  
  26.     }  
  27. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多