|  | Home · All Classes · Modules

# QTimer Class Reference
[QtCore module]

The QTimer class provides repetitive and single-shot timers. More...

Inherits QObject.

### Methods

- __init__ (self, QObject parent = None)
- int interval (self)
- bool isActive (self)
- bool isSingleShot (self)
- setInterval (self, int msec)
- setSingleShot (self, bool asingleShot)
- start (self, int msec)
- start (self)
- stop (self)
- timerEvent (self, QTimerEvent)
- int timerId (self)

### Static Methods

- singleShot (int msec, QObject receiver, SLOT()SLOT() member)
- singleShot (int msec, callable receiver)

### Qt Signals

- void timeout ()

## 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):

         QTimer *timer = new QTimer(this);
         connect(timer, SIGNAL(timeout()), this, SLOT(update()));
         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:

         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:

         QTimer *timer = new QTimer(this);
         connect(timer, SIGNAL(timeout()), this, SLOT(processOneThing()));
         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.

## Method Documentation

### QTimer.__init__ (self, QObject parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a timer with the given parent.

### int QTimer.interval (self)

### bool QTimer.isActive (self)

### bool QTimer.isSingleShot (self)

### QTimer.setInterval (self, int msec)

### QTimer.setSingleShot (self, bool asingleShot)

### QTimer.singleShot (int msec, QObject receiver, SLOT()SLOT() member)

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:

     #include <QApplication>
     #include <QTimer>

     int main(int argc, char *argv[])
     {
         QApplication app(argc, argv);
         QTimer.singleShot(600000, &app, SLOT(quit()));
         ...
         return app.exec();
     }

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.

Note: This function is reentrant.

See also setSingleShot() and start().

### QTimer.singleShot (int msec, callable receiver)

### QTimer.start (self, int msec)

This method is also a Qt slot with the C++ signature `void start(int)`.

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

If the timer is already running, it will be stopped and restarted.

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

### QTimer.start (self)

This method is also a Qt slot with the C++ signature `void start()`.

This function overloads start().

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

If the timer is already running, it will be stopped and restarted.

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

### QTimer.stop (self)

This method is also a Qt slot with the C++ signature `void stop()`.

Stops the timer.

See also start().

### QTimer.timerEvent (self, QTimerEvent)

Reimplemented from QObject.timerEvent().

### int QTimer.timerId (self)

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

## Qt Signal Documentation

### void timeout ()

This is the default overload of this signal.

This signal is emitted when the timer times out.

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