[Qt-interest] timers cannot be started from another thread
Thiago Macieira
thiago.macieira at trolltech.com
Mon Jul 20 18:41:57 CEST 2009
Em Segunda-feira 20 Julho 2009, às 18:18:55, Matthias Pospiech escreveu:
> In short: Is there any possibilty of having a timer within a thread?
Yes, you can, for objects that belong to that thread.
You can't start timers in objects that belong to other threads.
> 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"
Because that QTimer is created in the main thread. Its constructor is run
inside the constructor of your GratingVariationShowMovie class.
When created, an object belongs to the thread it was created in. Since you
created it in the main thread, that's where it belongs.
You should either create it in the actual thread (i.e., inside run()) or move
it there (moveToThread()).
> 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)
That happens because you passed "this" as parent. And, like I explained above,
objects belong to the thread they were created in. This applies even to
classes derived from QThread: those objects belong to the main thread.
You should either move the QThread to its own thread (i.e. moveToThread(this))
or you should not use a parent for your QTimer.
> QObject::killTimer: timers cannot be stopped from another thread
>
> confused....
Everything is explained by the "object belongs to a thread" mechanism.
--
Thiago Macieira - thiago.macieira (AT) nokia.com
Senior Product Manager - Nokia, Qt Software
Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090720/852d94e6/attachment.bin
More information about the Qt-interest-old
mailing list