com.trolltech.qt.core
Class QTimer

java.lang.Object
  extended by com.trolltech.qt.internal.QSignalEmitterInternal
      extended by com.trolltech.qt.QSignalEmitter
          extended by com.trolltech.qt.QtJambiObject
              extended by com.trolltech.qt.core.QObject
                  extended by com.trolltech.qt.core.QTimer
All Implemented Interfaces:
QtJambiInterface

public class QTimer
extends QObject

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

The following code example is written in c++.

    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, "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 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);

        timer.timeout.connect(this, "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.

Note that QTimer's accuracy depends on the underlying operating system and hardware. Most platforms support an accuracy of 1 millisecond, but Windows 98 supports only 55. If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.

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.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.PrivateSignal0, QSignalEmitter.PrivateSignal1, QSignalEmitter.PrivateSignal2, QSignalEmitter.PrivateSignal3, QSignalEmitter.PrivateSignal4, QSignalEmitter.PrivateSignal5, QSignalEmitter.PrivateSignal6, QSignalEmitter.PrivateSignal7, QSignalEmitter.PrivateSignal8, QSignalEmitter.PrivateSignal9, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9
 
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal
 
Field Summary
 QSignalEmitter.Signal0 timeout
           
 
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
currentSender
 
Constructor Summary
QTimer()
          Constructs a timer with the given parent.
QTimer(QObject parent)
          Constructs a timer with the given parent.
 
Method Summary
 int interval()
          This property holds the timeout interval in milliseconds.
 boolean isActive()
          This boolean property is true if the timer is running; otherwise false.
 boolean isSingleShot()
          This property holds whether the timer is a single-shot timer.
 void setInterval(int msec)
          This property holds the timeout interval in milliseconds.
 void setSingleShot(boolean singleShot)
          This property holds whether the timer is a single-shot timer.
static void singleShot(int msec, QObject obj, java.lang.String method)
          This static function calls a slot after a given time interval.
 void start()
          This function overloads start().
 void start(int msec)
          Starts or restarts the timer with a timeout interval of msec milliseconds.
 void stop()
          Stops the timer.
 int timerId()
          Returns the ID of the timer if the timer is running; otherwise returns -1.
 
Methods inherited from class com.trolltech.qt.core.QObject
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
__qt_signalInitialization
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Field Detail

timeout

public final QSignalEmitter.Signal0 timeout
Constructor Detail

QTimer

public QTimer()
Constructs a timer with the given parent.


QTimer

public QTimer(QObject parent)
Constructs a timer with the given parent.

Method Detail

interval

public final int interval()
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.


isActive

public final boolean isActive()
This boolean property is true if the timer is running; otherwise false.


isSingleShot

public final boolean isSingleShot()
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().


setInterval

public final void setInterval(int msec)
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.


setSingleShot

public final void setSingleShot(boolean singleShot)
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().


start

public final void start()
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.


start

public final void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.


stop

public final void stop()
Stops the timer.

See also:
start().


timerId

public final int timerId()
Returns the ID of the timer if the timer is running; otherwise returns -1.


singleShot

public static void singleShot(int msec,
                              QObject obj,
                              java.lang.String method)
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 QObject.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 obj is the receiving object and the member is the slot. The time interval is msec milliseconds.