QTimer Class Reference

The QTimer class provides repetitive and single-shot timers.

  1. #include <QTimer>

Inherits: QObject.

Detailed Description

The QTimer class provides repetitive and single-shot timers.

The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start(). From then on it will emit the timeout() signal at constant intervals.

Example for a one second (1000 millisecond) timer (from the Analog Clock example):

  1.     QTimer *timer = new QTimer(this);
  2.     connect(timer, SIGNAL(timeout()), this, SLOT(update()));
  3.     timer->start(1000);

From then on, the update() slot is called every second.

You can set a timer to time out only once by calling setSingleShot(true). You can also use the static QTimer::singleShot() function to call a slot after a specified interval:

  1.     QTimer::singleShot(200, this, SLOT(updateCaption()));

In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec(). Qt uses the timer's thread affinity to determine which thread will emit the timeout() signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.

As a special case, a QTimer with a timeout of 0 will time out as soon as all the events in the window system's event queue have been processed. This can be used to do heavy work while providing a snappy user interface:

  1.     QTimer *timer = new QTimer(this);
  2.     connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
  3.     timer->start();

processOneThing() will from then on be called repeatedly. It should be written in such a way that it always returns quickly (typically after processing one data item) so that Qt can deliver events to widgets and stop the timer as soon as it has done all its work. This is the traditional way of implementing heavy work in GUI applications; multithreading is now becoming available on more and more platforms, and we expect that zero-millisecond QTimers will gradually be replaced by QThreads.

Accuracy and Timer Resolution

Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

The accuracy of timers depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.

If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.

Alternatives to QTimer

An alternative to using QTimer is to call QObject::startTimer() for your object and reimplement the QObject::timerEvent() event handler in your class (which must inherit QObject). The disadvantage is that timerEvent() does not support such high-level features as single-shot timers or signals.

Another alternative to using QTimer is to use QBasicTimer. It is typically less cumbersome than using QObject::startTimer() directly. See Timers for an overview of all three approaches.

Some operating systems limit the number of timers that may be used; Qt tries to work around these limitations.

See also QBasicTimer, QTimerEvent, QObject::timerEvent(), Timers, Analog Clock Example, and Wiggly Example.

Properties

Toggle detailsactiveactive : const bool

This boolean property is true if the timer is running; otherwise false.

Access functions:

    Look up this member in the source code.

    Toggle detailsintervalinterval : int

    This property holds the timeout interval in milliseconds

    The default value for this property is 0. A QTimer with a timeout interval of 0 will time out as soon as all the events in the window system's event queue have been processed.

    Setting the interval of an active timer changes its timerId().

    See also singleShot.

    Access functions:

    Look up this member in the source code.

    Toggle detailssingleShotsingleShot : bool

    This property holds whether the timer is a single-shot timer

    A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.

    See also interval and singleShot().

    Access functions:

    Look up this member in the source code.

      Public Functions

      Toggle details QTimer

      QTimerQTimer ( QObject *parent=0 ) ( QObject *parent=0 )

      Constructs a timer with the given parent.

      Look up this member in the source code.

      Toggle details QTimer

      ~QTimer~QTimer () ()

      Destroys the timer.

      Look up this member in the source code.

      Toggle details void QTimer

      singleShotsingleShot ( int msec , QObject *receiver , const char *member ...) ( int msec , QObject *receiver , const char *member ) [static]

      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 timerEvent or create a local QTimer object.

      Example:

      1. #include <QApplication>
      2. #include <QTimer>
      3.  
      4. int main(int argc, char *argv[])
      5.  {
      6.     QApplication app(argc, argv);
      7.     QTimer::singleShot(600000, &app, SLOT(quit()));
      8.     ...
      9.     return app.exec();
      10. }

      This sample program automatically terminates after 10 minutes (600,000 milliseconds).

      The receiver is the receiving object and the member is the slot. The time interval is msec milliseconds.

      See also setSingleShot() and start().

      Look up this member in the source code.

      Toggle details void QTimer

      timerEventtimerEvent ( QTimerEvent *e ) ( QTimerEvent *e ) [virtual protected]

      Reimplemented from QObject::timerEvent().

      Look up this member in the source code.

      Toggle details int QTimer

      timerIdtimerId () ()const

      Returns the ID of the timer if the timer is running; otherwise returns -1.

      Look up this member in the source code.

      Toggle details QTimer

      QTimerQTimer ( QObject *parent , const char *name ) ( QObject *parent , const char *name ) Qt3-support

      Constructs a timer called name, with a parent.

      Look up this member in the source code.

      Signals

      Toggle details void QTimer

      timeouttimeout () () [signal]

      This signal is emitted when the timer times out.

      See also interval, start(), and stop().

      Look up this member in the source code.

      Public Slots

      Toggle details void QTimer

      startstart ( int msec ) ( int msec ) [slot]

      Starts or restarts the timer with a timeout interval of msec milliseconds.

      Look up this member in the source code.

      Toggle details void QTimer

      startstart () () [slot]

      This function overloads start().

      Starts or restarts the timer with the timeout specified in interval.

      If singleShot is true, the timer will be activated only once.

      Look up this member in the source code.

      Toggle details void QTimer

      stopstop () () [slot]

      Stops the timer.

      See also start().

      Look up this member in the source code.

      Toggle details void QTimer

      changeIntervalchangeInterval ( int msec ) ( int msec ) [slot] Qt3-support

      Use setInterval(msec) or start(msec) instead.

      Look up this member in the source code.

      Toggle details int QTimer

      startstart ( int msec , bool sshot ) ( int msec , bool sshot ) [slot] Qt3-support

      This function overloads start().

      Call setSingleShot(sshot) and start(msec) instead.

      Look up this member in the source code.

      Notes provided by the Qt Community
      Informative
      • 5

      Votes: 1

      Coverage: Qt library 4.7, Qt 4.8

      Picture of Diluka Diluka

      Hobby Entomologist
      23 notes

      Implement QTimer class

      1. //This class will inherit from QTimer
      2. class Timer : public QTimer
      3. {
      4.     //We will count all the time, that passed in miliseconds
      5.     long timePassed;
      6.  
      7.     Q_OBJECT
      8.  
      9.     public:
      10.  
      11.     explicit Timer(QObject *parent = 0) : QTimer(parent)
      12.     {
      13.         timePassed = 0;
      14.         connect(this, SIGNAL(timeout()), this, SLOT(tick()));
      15.     }
      16.     private slots:
      17.  
      18.     //this slot will be connected with the Timers timeout() signal.
      19.     //after you start the timer, the timeout signal will be fired every time,
      20.     //when the amount interval() time passed.
      21.     void tick()
      22.     {
      23.         timePassed+=interval(); //we increase the time passed
      24.         qDebug()<<timePassed; //and debug our collected time
      25.     }
      26. };
      27. //////////////////////////////////////////////////////////////////////////////////////////////////
      28. //Inside the main application set interval for timer
      29.  
      30. Timer * timer = new Timer(this);
      31. timer->setInterval(1000);
      32. timer->start();
      33. //////////////////////////////////////////////////////////////////////////////////////
      34. //Manually Stop Qtimer
      35.  
      36. timer->stop();

      [Revisions]