TimersQObject, 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 timer id. The timer will now "fire" every interval milliseconds, until you explicitly call QObject::killTimer() with the timer id. For this mechanism to work, the application must run in an event loop. You start an event loop with QApplication::exec(). When a timer fires, the application sends a QTimerEvent, and the flow of control leaves the event loop until the timer event is processed. This implies that a timer cannot fire while your application is busy doing something else. 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 is possible). The accuracy depends on the underlying operating system. Windows 95/98 has 55 millisecond (18.2 times per second) accuracy; other systems that we have tested (UNIX X11 and Windows NT) can handle 1 millisecond intervals. The main API for the timer functionality is QTimer. That class provides regular timers that emit a signal when the timer fires, and inherits QObject so that it fits well into the ownership structure of most GUI programs. The normal way of using it is like this: QTimer * counter = new QTimer( this ); connect( counter, SIGNAL(timeout()), this, SLOT(updateCaption()) ); counter->start( 1000 ); The counter timer is made into a child of this widget, so that when this widget is deleted, the timer is deleted too. Next, its timeout signal is connected to the slot that will do the work, and finally it's started. QTimer also provides a simple one-shot timer API. QButton uses this to show the button being pressed down and then (0.1 seconds later) be released when the keyboard is used to "press" a button, for example:
QTimer::singleShot( 100, this, SLOT(animateTimeout()) ); 0.1 seconds after this line of code is executed, the same button's animateTimeout() slot is called. Here is an outline of a slightly longer example that combines object communication via signals and slots with a QTimer object. It demonstrates how to use timers to perform intensive calculations in a single-threaded application without blocking the user interface.
// 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, just an // outline. 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(); } }
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Quarterly au hasardDes apparences personnalisées utilisant les feuilles de styleQt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com