QObject, the base class of all Qt objects, provides the basic timer support in Qt. With QObject::startTimer(), you start a timer with an interval in milliseconds as argument. The function returns a unique integer identifier for the timer, the so-called timer id. The timer will now "fire" every interval milliseconds, until you explicitely call QObject::killTimer() with the respective timer id.
Whenever a timer fires, it sends a timer event to the respective object. For this mechanism to work, the application has to run in an event loop. You start an event loop with QApplication::exec(). When a timer fires, the application wakes up and the flow of control leaves the event loop until the timer event is prosessed. A timer cannot fire while your application is busy doing calculations. In that case it will fire immediately after the program returned to the event loop. In other words: the accuracy of timers depends on the granularity of your application.
There is practically no upper limit for the interval value (more than one year). The accuracy depends on the underlying operating system. Windows 95 has 55 millisecond (18.2 times per second) accuracy; other systems that we have tested (UNIX X11, Windows NT and OS/2) can handle 1 millisecond intervals.
Usually you won't utilize timers directly, but use the class QTimer instead. QTimer provides a high-level programming interface with one-shot timers and timer signals instead of events.
Here is a framework for an example that combines object communication via signals and slots with a QTimer object. It demonstrates how to utilize timers to perform intensive calculations in a single-threaded application without blocking the user interface.
Example:
// // The Mandelbrot class uses a QTimer to calculate the mandelbrot // set one scanline at a time without blocking the CPU. // It inherits QObject to use signals and slots. // Calling start() starts the calculation. The done() signal is // emitted when it has finished. // Note that this example is not complete. Feel free to complete it. // class Mandelbrot : public QObject { Q_OBJECT // required for signals/slots public: Mandelbrot( QObject *parent=0, const char *name ); ... public slots: void start(); signals: void done(); private slots: void calculate(); private: QTimer timer; ... }; // // Constructs and initializes a Mandelbrot object. // Mandelbrot::Mandelbrot( QObject *parent=0, const char *name ) : QObject( parent, name ) { connect( &timer, SIGNAL(timeout()), SLOT(calculate()) ); ... } // // Starts the calculation task. The internal calculate() slot // will be activated every 10 milliseconds. // void Mandelbrot::start() { if ( !timer.isActive() ) // not already running timer.start( 10 ); // timeout every 10 ms } // // Calculates one scanline at a time. // Emits the done() signal when finished. // void Mandelbrot::calculate() { ... // perform the calculation for a scanline if ( finished ) { // no more scanlines timer.stop(); emit done(); } }
Copyright © 2000 Troll Tech | Trademarks | Qt version 2.1.0
|