diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2011-12-21 11:32:43 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-02 10:44:00 +0100 |
commit | ef60ed1c9dbc87d9f8401036cc254a9c45cd8ca8 (patch) | |
tree | 8a2016d4fc1ec5d6c3caed14cb1b9467895aa1ef /src/corelib/kernel/qtimer.cpp | |
parent | 05ca21411eaaf3ea5e9cc9d82ecfaa8753f9f319 (diff) |
Add Qt::TimerType and the QTimer::timerType property
The timer type will control the accuracy of the timer. By default, all
timers are CoarseTimers, which allows for +/- 5% interval adjustment.
PreciseTimers will not have any interval adjustments, VeryCoarseTimers
will have intervals adjusted to full second resolution.
Use QTimer::setTimerType() or the QTimer::singleShot() overload to
specify the type.
QObject::startTimer() now takes a Qt::TimerType argument which defaults
to Qt::CoarseTimer. QBasicTimer::startTimer() gets an overload that
takes a Qt::TimerType argument. The argument is unused for now, since
the QAbstractEventDispatcher interface needs to change (done in a
separate commit).
Author: Thiago Macieira <thiago.macieira@nokia.com>
Change-Id: I3100da5aa1fe17ec30b8644897d0fe6ec4a07f52
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qtimer.cpp')
-rw-r--r-- | src/corelib/kernel/qtimer.cpp | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index 9be661e42f..e89abade2c 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -134,7 +134,6 @@ QT_BEGIN_NAMESPACE {Analog Clock Example}, {Wiggly Example} */ - static const int INV_TIMER = -1; // invalid timer id /*! @@ -142,7 +141,7 @@ static const int INV_TIMER = -1; // invalid timer id */ QTimer::QTimer(QObject *parent) - : QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0) + : QObject(parent), id(INV_TIMER), inter(0), del(0), single(0), nulltimer(0), type(Qt::CoarseTimer) { } @@ -203,7 +202,7 @@ void QTimer::start() if (id != INV_TIMER) // stop running timer stop(); nulltimer = (!inter && single); - id = QObject::startTimer(inter); + id = QObject::startTimer(inter, Qt::TimerType(type)); } /*! @@ -257,18 +256,18 @@ class QSingleShotTimer : public QObject int timerId; public: ~QSingleShotTimer(); - QSingleShotTimer(int msec, QObject *r, const char * m); + QSingleShotTimer(int msec, Qt::TimerType timerType, QObject *r, const char * m); Q_SIGNALS: void timeout(); protected: void timerEvent(QTimerEvent *); }; -QSingleShotTimer::QSingleShotTimer(int msec, QObject *receiver, const char *member) +QSingleShotTimer::QSingleShotTimer(int msec, Qt::TimerType timerType, QObject *receiver, const char *member) : QObject(QAbstractEventDispatcher::instance()) { connect(this, SIGNAL(timeout()), receiver, member); - timerId = startTimer(msec); + timerId = startTimer(msec, timerType); } QSingleShotTimer::~QSingleShotTimer() @@ -318,6 +317,25 @@ QT_END_INCLUDE_NAMESPACE void QTimer::singleShot(int msec, QObject *receiver, const char *member) { + singleShot(msec, Qt::CoarseTimer, receiver, member); +} + +/*! \overload + \reentrant + This static function calls a slot after a given time interval. + + It is very convenient to use this function because you do not need + to bother with a \link QObject::timerEvent() timerEvent\endlink or + create a local QTimer object. + + The \a receiver is the receiving object and the \a member is the slot. The + time interval is \a msec milliseconds. The \a timerType affects the + accuracy of the timer. + + \sa start() +*/ +void QTimer::singleShot(int msec, Qt::TimerType timerType, QObject *receiver, const char *member) +{ if (receiver && member) { if (msec == 0) { // special code shortpath for 0-timers @@ -330,7 +348,7 @@ void QTimer::singleShot(int msec, QObject *receiver, const char *member) QMetaObject::invokeMethod(receiver, methodName.constData(), Qt::QueuedConnection); return; } - (void) new QSingleShotTimer(msec, receiver, member); + (void) new QSingleShotTimer(msec, timerType, receiver, member); } } @@ -361,8 +379,17 @@ void QTimer::setInterval(int msec) inter = msec; if (id != INV_TIMER) { // create new timer QObject::killTimer(id); // restart timer - id = QObject::startTimer(msec); + id = QObject::startTimer(msec, Qt::TimerType(type)); } } +/*! + \property QTimer::timerType + \brief controls the accuracy of the timer + + The default value for this property is \c Qt::CoarseTimer. + + \sa Qt::TimerType +*/ + QT_END_NAMESPACE |