当前位置:网站首页>Qt | 定时器的使用 QTimer
Qt | 定时器的使用 QTimer
2022-08-02 14:12:00 【华为云】
在 Qt 中使用定时器一般有三种方式:
一、直接使用 QObject 类提供的定时器。
1.在需要开启定时器的地方直接调用 startTimer();
该函数的声明为:int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);
该函数开启一个定时器,返回值是定时器的编号。
参数一为时间间隔,单位毫秒;
参数二为定时器的精确度:
Qt::PreciseTimer(精确的定时器,尽量保持毫秒精度,试图保持精确度在 1 毫秒);
Qt::CoarseTimer(粗略的定时器,尽量保持精度在所需的时间间隔 5%范围内);
Qt::VeryCoarseTimer(很粗略的定时器,只保留完整的第二精度,大约为 500 毫秒);
2.重载 void QObject::timerEvent ( QTimerEvent * event );
当定时器溢出时,会自动响应 timerEvent()函数。
在 timerEvent()函数中,通过 event->timerId()来确定是哪个定时器触发的;
3.在需要关闭定时器的地方调用 killTimer();
该函数的声明为: void killTimer(int Id);
该函数关闭一个定时器,参数为定时器的编号。
二、使用 QTimer 类。
1.用 new 的方式创建一个 QTimer 对象。
QTimer *timer = new QTimer(this);
2.将定时器的溢出信号连接到自定义的槽函数。
connect(timer, &QTimer::timeout, this, &Myself::update);
3.启动定时器。
timer->start(1000);
函数原型为:void start(int msec);参数为定时器时间间隔,单位毫秒。
也可以调用 timer->setInterval(1000);设置定时器时间间隔,然后调用 timer->start();开启定时器。
4.停止定时器。
timer->stop();
三、仅调用一次溢出的定时器。
QTimer::singleShot(1000, this, SLOT(OnSendBreath()));
函数原型有两个:
1.static void singleShot(int msec, const QObject *receiver, const char *member);
参数一为时间间隔,单位毫秒;参数二为接收溢出信号的对象;参数三为溢出信号的槽函数;
2.static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member);
参数一为时间间隔,单位毫秒;参数二为定时器的精确度(同上文);参数三为接收溢出信号的对象;参数四为溢出信号的槽函数。
另:
都可以用到的一些函数:
判断定时器是否正在运行:bool QTimer::isActive () const
改变定时器的时间间隔:void QTimer::changeInterval ( int msec )
如果这个定时器正在运行,他将被停止并且重新开始,否则将会被开始。
边栏推荐
猜你喜欢

基于最小二乘法的线性回归分析方程中系数的估计

Open the door to electricity "Circuit" (3): Talk about different resistance and conductance

STM32LL library - USART interrupt to receive variable length information

6.统一记录日志

极简式 Unity 获取 bilibili 直播弹幕、SC、上舰、礼物等 插件

3.用户上传头像

Redis common interview questions

Based on the matrix calculation in the linear regression equation of the coefficient estimates

pygame draw arc

Unity-PlayMaker
随机推荐
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
3.用户上传头像
Unity-Post Processing
动态规划理论篇
unity-shader(中级)
【离散化+前缀和】Acwing802. 区间和
戴森球计划这个游戏牛逼
golang内存相关文章-收集
剑指offer:合并两个排序的链表
pygame image rotate continuously
Flink + sklearn - use JPMML implement flink deployment on machine learning model
关于分布式的一些知识点
Problems related to prime numbers - small notes
5.事务管理
2.登录退出,登录状态检查,验证码
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
3. User upload avatar
unity Domain Reload & scene Reload 静态变量重置
unity 和C# 一些官方优化资料
质数相关问题-小记