博客
关于我
10.QT-定时器
阅读量:442 次
发布时间:2019-03-06

本文共 1859 字,大约阅读时间需要 6 分钟。

QObject定时器

  • 需要头文件#include <QTimerEvent>

 需要函数

int QObject::startTimer(int interval);//启动定时器,启动后,就会一直定时触发timerEvent事件,设置定时器间隔时间(单位ms),启动后返回该定时器ID号.void QObject::timerEvent(QTimerEvent * event);//定时器处理函数,需要用户来重写它,如果有多个定时器,可以通过QTimerEvent::timerId()来判断定时器ID处理void killTimer(int id);//通过定时器ID号来杀掉某个定时器

示例代码

Widget.h:

#ifndef WIDGET_H#define WIDGET_H#include 
#include
class Widget : public QWidget{ Q_OBJECTprivate:virtual void timerEvent( QTimerEvent * event );public: explicit Widget(QWidget *parent = 0);};#endif // WIDGET_H

Widget.cpp:

#include "Widget.h"#include 
#include
Widget::Widget(QWidget *parent) : QWidget(parent){ int TimerID = startTimer(1000); //设置1000ms为单位 qDebug()<<"startTimerID : "<
timerId();}

 

QTimer定时器

 需要头文件#include <QTimer>  

QTimer类定时器支持单次触发和多次触发。

 

使用QTimer类定时器的步骤:

1. 创建一个QTimer定时器(示例)

QTimer *timer = new QTimer(this);

2. 连接timeout()信号与槽函数(示例)

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

3.启动定时器,并设置间隔时间

timer->start (int msec);               //单位ms

4.停止定时器

timer->stop();

5.删除定时器

delete timer;

 

常用函数

void   setSingleShot(bool singleShot);// 设置使能单次触发和多次触发,默认情况为多次触发bool   isActive();//判断定时器是否运行bool  setInterval ( int msec );//从新设置间隔时间

示例代码

Widget.h:

#ifndef WIDGET_H#define WIDGET_H#include 
#include
class Widget : public QWidget{ Q_OBJECT QTimer *timer;private slots: void time_handler();public: explicit Widget(QWidget *parent = 0);};#endif // WIDGET_H

Widget.cpp:

#include "Widget.h"#include 
#include
Widget::Widget(QWidget *parent) : QWidget(parent){ QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(time_handler())); timer->start(1000); //1000ms}void Widget::time_handler(){ qDebug()<<"Timer out";}

 

转载地址:http://pgiyz.baihongyu.com/

你可能感兴趣的文章
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
Nginx+Redis+Ehcache:大型高并发与高可用的三层缓存架构总结
查看>>
nginx+tomcat+memcached
查看>>
nginx+tomcat单个域名及多个域名配置
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>