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

Hannu Shemeikka hps at shemeikka.org
Thu May 6 10:05:19 CEST 2010


On Thu, 2010-05-06 at 09:59 +0200, Eirik Ulvik wrote:
> 
> Den 06.05.2010 09:55, skrev Hannu Shemeikka:
> > Hi,
> > 
> > You could use something like this:
> > 
> > timer = new QTimer(this);
> > connect(this->timer, SIGNAL(timeout()), rThread, SLOT(yourSlotName()));
> > this->timer->start(2000); // 1 = 1 ms
> > 
> > This fires slot 'yourSlotName' in rThread (another QThread class) every
> > 2 seconds.
> > 
> > Or in the constructor of your thread:
> > 
> > timer = new QTimer(this);
> > connect(this->timer, SIGNAL(timeout()), this, SLOT(yourSlotName()));
> > this->timer->start(2000); // 1 = 1 ms
> > 
> If you want to do this in its own thread remember to start the threads
> own event loop in the run function ie.:
> 
> class MyThread : public QThread
> run()
> {
>   exec();
> }
> 
> There is also some problems with this approach. To get it working I used
> the recipe found here:
> http://lists.trolltech.com/qt-interest/2007-09/thread00073-0.html.
> 
> Good luck
> Eirik

Yeah,

I forgot that event loop. Can't remember everything :)

- Hannu




More information about the Qt-interest-old mailing list