[Qt-interest] timers cannot be started from another thread

Patric userqt at gmail.com
Wed Jul 22 10:14:26 CEST 2009


Timers requires an event loop. Therefore, your second thread should have 
such. Also if a timer is created in one thread, it can't be started from 
another.
As far as I remember...

Regards,
Patric

----- Original Message ----- 
From: "Matthias Pospiech" <matthias.pospiech at gmx.de>
To: <qt-interest at trolltech.com>
Sent: Monday, July 20, 2009 7:18 PM
Subject: [Qt-interest] timers cannot be started from another thread


> In short: Is there any possibilty of having a timer within a thread?
>
>
> I want to have a Timer in a QThread.
>
> The thread is working with some devices such as translationstages.
> Parallel I have a second setup which I want to change at defined time
> intervalls.
>
> Therefore I set up the following (simplified):
>
> class GratingVariationShowMovie: public QThread
> {
>    Q_OBJECT
> public:
>    GratingVariationShowMovie()        {    }
>
>    virtual ~GratingVariationShowMovie(){}
>
> private:
>    QTimer powerTimer;
>
> private slots:
>    void OnPowerTimerTimeout()
>    {
>        double destination =
> powerTimeline[powerTimelineIndex].powerPosition;
>        RotationStage->PowerOn();
>        RotationStage->move(destination / RotationStage->CountsToDegree());
>
>
> powerTimer.setInterval(powerTimeline[powerTimelineIndex].timeUntilChange);
>        powerTimer.start();
>        powerTimelineIndex++;
>    }
>
> public:
>
>    void run()
>    {
>        connect(&powerTimer, SIGNAL(timeout()), this,
> SLOT(OnPowerTimerTimeout()), Qt::DirectConnection);
>        runThread();
>    }
>
>    void runThread()
>    {
>
>        OnPowerTimerTimeout();
>        ...(main activity)
>    }
>
> };
>
> This however gives me the error "timers cannot be started from another
> thread"
>
> If I change the timer to a pointer and construct it in the run() method,
> then I get the error:
>
> QObject: Cannot create children for a parent that is in a different 
> thread.
> (Parent is GratingVariationShowMovie(0x1efe100), parent's thread is
> QThread(0xe1
> 7080), current thread is GratingVariationShowMovie(0x1efe100)
> QObject::killTimer: timers cannot be stopped from another thread
>
> confused....
>
> Matthias
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 




More information about the Qt-interest-old mailing list