首页 技术 正文
技术 2022年11月20日
0 收藏 823 点赞 4,826 浏览 1567 个字

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);
  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. }
  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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918