QTimer Start Time

I try to start the timer at a specific time, for example, 02:30. Every day starts at 02.30.

Is it possible? Do you have any ideas?

Many thanks.

+8
c ++ qt
source share
3 answers

QTimer does not process a specific time of day initially, but you can use it with QDateTime to get what you want. That is, use QDateTime objects to find out how many seconds are between (right now) and 2:30 (QDateTime :: msecsTo () looks especially appropriate here), and then set QTimer to turn off after that a few seconds. Repeat as necessary.

+9
source share

Depending on the required resolution, you can use the usual QTimer, which works, let them speak every minute.
In timerEvent, you can check if you are at the right time (using QDateTime) and fire the necessary event.

+6
source share

Jeremy’s decision is really elegant, but it does not take into account daylight saving time. To protect yourself from this, you must trigger a timer event every hour and check the wall clock. Calculate the delta of the target, as suggested by Jeremy, and if it falls within the next hour, set a timer for shooting and turn off the hourly timer.
If not, just wait until the hourly timer lights up 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 
+1
source share

All Articles