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 at regular intervals 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. In multithreaded applications, you can use the timer mechanism 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 object's thread affinity to determine which thread will deliver the QTimerEvent. Because of this, you must start and stop all timers in the object's thread; it is not possible to start timers for objects in another thread. 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 98 has 55 millisecond accuracy; other systems that we have tested 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 *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateCaption())); timer->start(1000); The QTimer object 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 static function for single-shot timers. For example: QTimer::singleShot(200, this, SLOT(updateCaption())); 200 milliseconds (0.2 seconds) after this line of code is executed, the updateCaption() slot will be called. For QTimer to work, you must have an event loop in your application; that is, you must call QCoreApplication::exec() somewhere. Timer events will be delivered only while the event loop is running. 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. The Analog Clock example shows how to use QTimer to redraw a widget at regular intervals. From AnalogClock's implementation: AnalogClock::AnalogClock(QWidget *parent) : QWidget(parent) { QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000); ... } Every second, QTimer will call the QWidget::update() slot to refresh the clock's display. If you already have a QObject subclass and want an easy optimization, you can use QBasicTimer instead of QTimer. With QBasicTimer, you must reimplement timerEvent() in your QObject subclass and handle the timeout there. The Wiggly example shows how to use QBasicTimer. |
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. 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 4.1 | |
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