码迷,mamicode.com
首页 > 其他好文 > 详细

QT定时器的两种应用(QObject就有timerEvent事件,一种什么样的居心呢?)

时间:2017-12-07 23:46:57      阅读:469      评论:0      收藏:0      [点我收藏+]

标签:dialog   sdn   text   type   ref   alt   data   void   情况下   

QT中定时器的使用方法
(1)重载timerEvent(QTimerEvent *)函数,然后再在类的构造函数中设置时间间隔
   startTimer(50);//单位为毫秒
(2)在类的构造函数中设定如下:
   QTimer *timer=new QTimer(this);
   connect(timer,SIGNAL(timeout()),this,SLOT(timeoutslot()));//timeoutslot()为自定义槽
   timer->start(1000);
 
 
QT定时器的两种应用
2009-10-14 8:44

可以用槽函数实现

(1)重载timerEvent(QTimerEvent *)函数,然后再在类的构造函数中设置时间间隔
startTimer(50);//单位为毫秒
(2)在类的构造函数中设定如下:

  1. QTimer *timer=new QTimer(this);
  2. connect(timer,SIGNAL(timeout()),this,SLOT(timeoutslot()));//timeoutslot()为自定义槽
  3. timer->start(1000);
[c++] view plaincopy
 
 
  1. QTimer *timer=new QTimer(this);  
  2.    connect(timer,SIGNAL(timeout()),this,SLOT(timeoutslot()));//timeoutslot()为自定义槽  
  3.    timer->start(1000);  

然而:所有Qobject的子类在设置定时器时都不必加载一个Qtimer对象,因为这样造成了资源浪费且需要书写多余的函数,很不方便.最好的办法是重载timerEvent函数,具体写法如下:

  1. class Gui_DlgViewCtrlDatum : public QDialog
  2. {
  3. Q_OBJECT
  4. public:
  5. Gui_DlgViewCtrlDatum( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
  6. ~Gui_DlgViewCtrlDatum();
  7. protected:
  8. void timerEvent( QTimerEvent * );
  9. };
  10. void Gui_DlgViewCtrlDatum::timerEvent( QTimerEvent *e )
  11. {
  12. //statements
  13. }
[c++] view plaincopy
 
 
  1. class Gui_DlgViewCtrlDatum : public QDialog  
  2. {  
  3. Q_OBJECT  
  4. public:  
  5. Gui_DlgViewCtrlDatum( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );  
  6. ~Gui_DlgViewCtrlDatum();  
  7. protected:  
  8. void timerEvent( QTimerEvent * );  
  9. };  
  10. void Gui_DlgViewCtrlDatum::timerEvent( QTimerEvent *e )  
  11. {  
  12. //statements  
  13. }  

再在Gui_DlgViewCtrlDatum的构造函数中设置时间间隔:
startTimer(50);//单位为毫秒

这样,每隔50毫秒,函数timerEvent便会被调用一次.

网上又说:

定时器事件的优先级很低,如果需要多个定时器,那么跟踪每一个定时器的ID是很费时的。这种情况下,较好的方法是为每一个定时器创建一个QTimer对象。在每一个时间间隔内,QTimer发出一个timeout()信号。QTimer还支持一次性定时器(只发出一次timeout()信号的定时器)。

 

 

http://blog.csdn.net/u013394556/article/details/42775213

QT定时器的两种应用(QObject就有timerEvent事件,一种什么样的居心呢?)

标签:dialog   sdn   text   type   ref   alt   data   void   情况下   

原文地址:http://www.cnblogs.com/findumars/p/8001373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!