开发者

QTimer start specific time

I try to start timer at specific time like 02:30. Every day it starts at 02.30.

Is it possible? Do you have an开发者_开发知识库y idea?

Thank a lot.


QTimer doesn't handle specific times of day natively, but you could use it in conjunction with QDateTime to get what you want. That is, use QDateTime objects to figure out how many seconds are between (right now) and 2:30 (QDateTime::msecsTo() looks particularly appropriate here), then set your QTimer to go off after that many seconds. Repeat as necessary.


Depending on the required resolution, you could use an ordinary QTimer that fires let's say every minute.
In the timerEvent, you could check if you are on the right time (using QDateTime), and trigger the necessary event.


The solution of Jeremy is indeed elegant, but it doesn't take into account the daylight savings time. To guard against that, you should fire a timer event every hour and check the wall clock. Calculate the delta to the target, like Jeremy proposes, and if it falls within the coming hour, set a timer to fire, and disable the hourly timer.
If not, just wait for the hourly timer to fire again.

Pseudo code:

Get wall clock time
Calculate difference between target time and wall clock
If difference < 1 hour:
    Set timer to fire after difference secs
    If this is a repeating event, restart the hourly timer
Else:
    Start watch timer to do this calculation again after one hour
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜