简体   繁体   English

QTimer长时间超时

[英]QTimer long timeout

Qt 5.7 32-bit on windows 10 64-bit long period timer Windows 10 64位长周期计时器上的Qt 5.7 32位

the interval of a QTimer is given in msecs as a signed integer, so the maximum interval which can be set is a little bit more than 24 days (2^31 / (1000*3600*24) = 24.85) I need a timer with intervals going far beyond this limit. QTimer的间隔以毫秒为单位作为有符号整数给出,因此可以设置的最大间隔略大于24天(2 ^ 31 /(1000 * 3600 * 24)= 24.85)我需要一个计时器间隔远远超出此限制。 So my question is, which alternative do you recommend? 所以我的问题是,您推荐哪种选择? std::chrono (C++11) seems not to be suitable as it does not have an event handler? std :: chrono(C ++ 11)似乎不适合,因为它没有事件处理程序?

Alain 阿兰

You could always create your own class which uses multiple QTimer's for the duration they are valid and just count how many have elapsed. 您始终可以创建自己的类,该类在有效期间使用多个QTimer,并仅计算已使用的数量。

Pretty simple problem. 很简单的问题。 If you can only count to 10 and you need to count to 100 - just count to 10 ten times. 如果您只能数到10,而又需要数到100-只需数10即可。

I would implement this in the following way: 我将通过以下方式实现此目的:

Upon timer start, note the current time in milliseconds like this: 计时器启动后,请以毫秒为单位记录当前时间,如下所示:

m_timerStartTime = QDateTime::currentMSecsSinceEpoch()

The, I would start a timer at some large interval, such as 10 hours, and attach a handler function to the timer that simply compared the time since it started to see if we are due: 我将以一个较大的间隔(例如10小时)启动一个计时器,并将一个处理函数附加到计时器上,该功能可以简单地比较自计时器开始计时以来的时间,以查看是否到期:

 if(QDateTime::currentMSecsSinceEpoch() - m_timerStartTime > WANTED_DELAY_TIME){
      // Execute the timer payload
      // Stop interval timer
 }

This simple approach could be improved in several ways. 这种简单的方法可以通过几种方式进行改进。 For example, to keep the timer running even if application is stopped/restarted, simply save the timer start time in a setting or other persistent storage, and read it back in at application start up. 例如,即使在应用程序停止/重新启动时也要保持计时器运行,只需将计时器的开始时间保存在设置或其他持久性存储中,并在应用程序启动时将其读回即可。

And to improve precision, simply change the interval from the timer handler function in the last iteration so that it tracks the initial end time perfectly (instead of overshooting by up to 10 minutes). 为了提高精度,只需在上次迭代中更改计时器处理程序函数的时间间隔,即可完美跟踪初始结束时间(而不是长达10分钟的超调时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM