使用QT定时器遇到的问题

问题描述:
程序上有槽函数触发频率变化的需求。在使用Qt定时器触发槽函数的过程中发现,每秒钟1000次没有问题,在每秒钟30~50次的情况下出现了延迟。
主体代码如下:

    std::chrono::time_point lastTime = std::chrono::high_resolution_clock::now();
    m_grabImgTimer = new QTimer();
    connect(m_grabImgTimer, SIGNAL(timeout()), this, SLOT(TriggerGrabImaSlot()));
    m_grabImgTimer->start(1000/ 30);
void TriggerGrabImaSlot()
{
    std::chrono::time_point nowTime = std::chrono::high_resolution_clock::now();

    std::chrono::duration<double, std::milli> fp_ms = nowTime - lastTime;
    lastTimer = nowTime;
    std::cout << fp_ms.count() << " ms"<<std::endl;
}

10次的时候控制台打印
在这里插入图片描述

30次的时候控制台打印
在这里插入图片描述
造成原因:
在这里插入图片描述
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的定时器,那么在创建定时器的时候,需要更改一下时钟的类型。

m_grabImgTimer->setTimerType(Qt::PreciseTimer);
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值