path: root/src/corelib/kernel/qtimerinfo_unix.cpp
diff options
authorLaszlo Papp <>2012-02-23 07:41:30 +0200
committerQt by Nokia <>2012-04-03 15:10:05 +0200
commit8b56b8ed32cf6351047b21c80359f5f2b895a314 (patch)
tree840ebb7726fd527684c38496bd6edcdf0712c06f /src/corelib/kernel/qtimerinfo_unix.cpp
parent4b023e248747a589f2402b03f1967b999e3bb3b8 (diff)
Add a remainingTime() method to the public interface of the QTimer class
It is an extension coming from the use case when you, for instance, need to implement a countdown timer in client codes, and manually maintain a dedicated variable for counting down with the help of yet another Timer. There might be other use cases as well. The returned value is meant to be in milliseconds, as the method documentation says, since it is reasonable, and consistent with the rest (ie. the interval accessor). The elapsed time is already being tracked inside the event dispatcher, thus the effort is only exposing that for all platforms supported according to the desired timer identifier, and propagating up to the QTimer public API. It is done by using the QTimerInfoList class in the glib and unix dispatchers, and the WinTimeInfo struct for the windows dispatcher. It might be a good idea to to establish a QWinTimerInfo (qtimerinfo_win{_p.h,cpp}) in the future for resembling the interface for windows with the glib/unix management so that it would be consistent. That would mean abstracting out a base class (~interface) for the timer info classes. Something like that QAbstractTimerInfo. Test: Build test only on (Arch)Linux, Windows and Mac. I have also run the unit tests and they passed as well. Change-Id: Ie37b3aff909313ebc92e511e27d029abb070f110 Reviewed-by: Thiago Macieira <> Reviewed-by: Bradley T. Hughes <>
Diffstat (limited to 'src/corelib/kernel/qtimerinfo_unix.cpp')
1 files changed, 31 insertions, 0 deletions
diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp
index 32e24edcf3..709338afda 100644
--- a/src/corelib/kernel/qtimerinfo_unix.cpp
+++ b/src/corelib/kernel/qtimerinfo_unix.cpp
@@ -412,6 +412,37 @@ bool QTimerInfoList::timerWait(timeval &tm)
return true;
+ Returns the timer's remaining time in milliseconds with the given timerId, or
+ null if there is nothing left. If the timer id is not found in the list, the
+ returned value will be -1. If the timer is overdue, the returned value will be 0.
+int QTimerInfoList::timerRemainingTime(int timerId)
+ timeval currentTime = updateCurrentTime();
+ repairTimersIfNeeded();
+ timeval tm = {0, 0};
+ for (int i = 0; i < count(); ++i) {
+ register QTimerInfo *t = at(i);
+ if (t->id == timerId) {
+ if (currentTime < t->timeout) {
+ // time to wait
+ tm = roundToMillisecond(t->timeout - currentTime);
+ return tm.tv_sec*1000 + tm.tv_usec/1000;
+ } else {
+ return 0;
+ }
+ }
+ }
+#ifndef QT_NO_DEBUG
+ qWarning("QTimerInfoList::timerRemainingTime: timer id %i not found", timerId);
+ return -1;
void QTimerInfoList::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *object)
QTimerInfo *t = new QTimerInfo;