当前位置: 首页 > news >正文

Qt 精确定时器

头图

Qt 精确定时器

文章目录

  • Qt 精确定时器
    • 摘要
    • 1 QTimer 定时器类型选择
    • 2 第一种精确定时器
    • 3 第二种线程定时器

关键字: Qt定时器Qtimer线程精度

摘要

众所周知,Qt QTimer定时的精度,那是稀里糊涂呀,所以在遇到需要高精度的定时器的时候,就没法直接只用QTimer定时器了,只能自己搞一个了。今天分享两种方式,一种是利用Win的相关API实现的,也是Google来的这里做一下分享吧。一种就是线程了,其实原理很简单,就是一个线程开while,靠睡眠时间来触发信号。

1 QTimer 定时器类型选择

其实QTimer定时器也是有进度可以选着的。官网描述如下,如果可以接受的话,其实可以通过定时器类型来确定,包括我后期使用中,其实大部分还是使用了Qt的官方定时器。

enum Qt::TimerType

The timer type indicates how accurate a timer can be.

ConstantValueDescription
Qt::PreciseTimer0Precise timers try to keep millisecond accuracy
Qt::CoarseTimer1Coarse timers try to keep accuracy within 5% of the desired interval
Qt::VeryCoarseTimer2Very coarse timers only keep full second accuracy

On UNIX (including Linux, macOS, and iOS), Qt will keep millisecond accuracy for Qt::PreciseTimer. For Qt::CoarseTimer, the interval will be adjusted up to 5% to align the timer with other timers that are expected to fire at or around the same time. The objective is to make most timers wake up at the same time, thereby reducing CPU wakeups and power consumption.

On Windows, Qt will use Windows’s Multimedia timer facility (if available) for Qt::PreciseTimer and normal Windows timers for Qt::CoarseTimer and Qt::VeryCoarseTimer.

On all platforms, the interval for Qt::VeryCoarseTimer is rounded to the nearest full second (e.g. an interval of 23500ms will be rounded to 24000ms, and 20300ms to 20000ms).

Qt::PreciseTimer 毫米级高精度定时器

Qt::CoarseTimer 粗略计时器,进度控制在5%以内

Qt::VeryCoarseTimer 非常粗糙的定时器,这个基本是个废废吧

2 第一种精确定时器

这个就是参考Windows下多媒体定时器为改造的一个高性能定时器。弊端呢就是,这个是没法夸平台的

Pro中添加一下代码

LIBS += -lwinmm

这里就直接上代码

#ifndef TURING_WIN_PERFORMANCETIMER_H
#define TURING_WIN_PERFORMANCETIMER_H

#include <QObject>

#include "windows.h"
/**
 * @brief The Turing_Win_PerformanceTimer class
 * Win平台下,高精度毫秒级定时器
 * 这里使用了win的api,所以在linux下无法使用
 */
class Turing_Win_PerformanceTimer : public QObject
{
    Q_OBJECT
public:
    explicit Turing_Win_PerformanceTimer(QObject *parent = nullptr);
    ~Turing_Win_PerformanceTimer();

signals:
    void timeout();
public slots:
    void start(int timeInterval);
    void stop();
    friend WINAPI void  CALLBACK PeriodCycle(uint,uint,DWORD_PTR,DWORD_PTR,DWORD_PTR);
private:
    int m_interval;
    int m_id;

};

#endif // TURING_WIN_PERFORMANCETIMER_H
#include "turing_win_performancetimer.h"




#ifdef __MINGW32__
#define TIME_KILL_SYNCHRONOUS 0x0100
#endif

Turing_Win_PerformanceTimer::Turing_Win_PerformanceTimer(QObject *parent)
    : QObject{parent}
{
    m_id=0;
}

Turing_Win_PerformanceTimer::~Turing_Win_PerformanceTimer()
{
    stop();
}
void  CALLBACK PeriodCycle(uint timerId,uint,DWORD_PTR user,DWORD_PTR,DWORD_PTR)
{
    Q_UNUSED(timerId)
    Turing_Win_PerformanceTimer *t=reinterpret_cast<Turing_Win_PerformanceTimer *>(user);
    emit t->timeout();
}
void Turing_Win_PerformanceTimer::start(int timeInterval)
{
    m_id = timeSetEvent(timeInterval,1,PeriodCycle,(DWORD_PTR)this,TIME_CALLBACK_FUNCTION|TIME_PERIODIC|TIME_KILL_SYNCHRONOUS);
}
void Turing_Win_PerformanceTimer::stop()
{
    if(m_id)
    {
        timeKillEvent(m_id);
        m_id=0;
    }
}


3 第二种线程定时器

这里也直接上代码,这种感觉有点浪费资源。

class SpeedThread :  public QThread
{
    Q_OBJECT
public:
    /**
     * @brief run
     * 重载线程函数
     */
    virtual void run();
signals:
    void signalselfInspection(int number);
    void signalSelfInspectionfinished();
public:
    bool runFlag = true;
private:
    int m_number = 0;
    bool flagDESC = false;

};
void SpeedThread::run()
{
    while (runFlag)
    {
        emit signalselfInspection(m_number);
        if(flagDESC)
        {
            if(m_number == 0)
            {
                runFlag = !runFlag;
                flagDESC = !flagDESC;
                emit signalSelfInspectionfinished();
            }
            m_number--;
        }
        else
        {
            m_number++;
            if(m_number == 100)
            {
                flagDESC = !flagDESC;
            }
        }
        msleep(8);
    }
}

博客签名2021

相关文章:

  • mysql中int(11)与int(1)的区别与联系
  • 【Vue】Vue的v-for指令
  • 【Hadoop---09】MapReduce:MapReduce概述
  • 黑马旅游网-配置项目(一)
  • 【C语言】continue 关键字
  • java基于springboot+Vue+nodejs的高校网上二手闲置跳蚤市场 element
  • JAVA计算机毕业设计超市进销存管理系统Mybatis+系统+数据库+调试部署
  • Java中ArrayList对象中trimToSize()方法具有什么功能呢?
  • B01-快速入门CSS
  • makefile的基础规则与命名方式
  • 自动驾驶仿真:角雷达坐标系转换详解
  • 教你如何制作浪漫的3D相册表白网站 HTML+CSS+JavaScript
  • 【Shell编程】Shell中for循环、while循环、until循环语句
  • Hadoop集群配置运行
  • 【数模/预测】灰色预测
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • 0基础学习移动端适配
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • js作用域和this的理解
  • laravel 用artisan创建自己的模板
  • 包装类对象
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 机器学习学习笔记一
  • 京东美团研发面经
  • 面试总结JavaScript篇
  • 使用Swoole加速Laravel(正式环境中)
  • 数组的操作
  • hi-nginx-1.3.4编译安装
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • #Linux(make工具和makefile文件以及makefile语法)
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (12)目标检测_SSD基于pytorch搭建代码
  • (a /b)*c的值
  • (Forward) Music Player: From UI Proposal to Code
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (七)理解angular中的module和injector,即依赖注入
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (新)网络工程师考点串讲与真题详解
  • ***原理与防范
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道
  • .NET 发展历程
  • .NET 回调、接口回调、 委托
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • .NET文档生成工具ADB使用图文教程
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • @Validated和@Valid校验参数区别