[Qt-interest] how to set up loop with timer

Guido Seifert Wargand at gmx.de
Thu May 6 10:00:49 CEST 2010



> Yes i was reading the docs but there is a lots of options to implement
> timers
> what will be the simple way to to that ?
> you have the :
> QObject::startTimer also
> 

Whether you use the QObject::startTimer or a QTimer is mainly a matter of taste. One uses the events, the other signal/slots. 

If you want to use QObject::startTimer there is a nice example in the docs:
class MyObject : public QObject
 {
     Q_OBJECT

 public:
     MyObject(QObject *parent = 0);

 protected:
     void timerEvent(QTimerEvent *event);
     int id;
 };

 MyObject::MyObject(QObject *parent)
     : QObject(parent)
 {
     id = startTimer(1000);   // 1-second timer
 }

 void MyObject::timerEvent(QTimerEvent *event)
 {
     if(id == event->timerId())
       excecute_my_code_once_every_second();
 }

I 'upgraded' the example a little with the id variable.

Guido



-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01



More information about the Qt-interest-old mailing list