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

Matthias Pospiech matthias.pospiech at gmx.de
Mon Jul 20 18:18:55 CEST 2009


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



More information about the Qt-interest-old mailing list