\snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 8
- Note that QTimer's accuracy depends on the underlying operating
- system and hardware. Most platforms support an accuracy of 20
- milliseconds; some provide more. If Qt is unable to deliver the
- requested number of timer events, it will silently discard some.
+ Note that QTimer's accuracy depends on the underlying operating system and
+ hardware. The \a timerType argument allows you to customize the accuracy of
+ the timer. See Qt::TimerType for information on the different timer types.
+ Most platforms support an accuracy of 20 milliseconds; some provide more.
+ If Qt is unable to deliver the requested number of timer events, it will
+ silently discard some.
The QTimer class provides a high-level programming interface with
single-shot timers and timer signals instead of events. There is
\sa timerEvent(), killTimer(), QTimer::singleShot()
*/
-int QObject::startTimer(int interval)
+int QObject::startTimer(int interval, Qt::TimerType timerType)
{
Q_D(QObject);
{Analog Clock Example}, {Wiggly Example}
*/
-
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)
{
}
if (id != INV_TIMER) // stop running timer
stop();
nulltimer = (!inter && single);
- id = QObject::startTimer(inter);
+ id = QObject::startTimer(inter, Qt::TimerType(type));
}
/*!
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()
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
QMetaObject::invokeMethod(receiver, methodName.constData(), Qt::QueuedConnection);
return;
}
- (void) new QSingleShotTimer(msec, receiver, member);
+ (void) new QSingleShotTimer(msec, timerType, receiver, member);
}
}
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
Q_OBJECT
Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot)
Q_PROPERTY(int interval READ interval WRITE setInterval)
+ Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType)
Q_PROPERTY(bool active READ isActive)
public:
explicit QTimer(QObject *parent = 0);
void setInterval(int msec);
int interval() const { return inter; }
+ void setTimerType(Qt::TimerType type) { this->type = type; }
+ Qt::TimerType timerType() const { return Qt::TimerType(type); }
+
inline void setSingleShot(bool singleShot);
inline bool isSingleShot() const { return single; }
static void singleShot(int msec, QObject *receiver, const char *member);
+ static void singleShot(int msec, Qt::TimerType timerType, QObject *receiver, const char *member);
public Q_SLOTS:
void start(int msec);
int id, inter, del;
uint single : 1;
uint nulltimer : 1;
+ uint type : 2;
+ // reserved : 28
};
inline void QTimer::setSingleShot(bool asingleShot) { single = asingleShot; }