PDA

View Full Version : Attempt to create QTimer results in: Cannot create children for a parent that is...



sebastian.f
30th April 2009, 16:25
Hello,

I am attempting to create a GUI control for use in future Qt applications. Part of it involves repeated calls of a function, while the mouse is down.

My control consists of a class "control" based on QWidget.

Within this there is another class "myqslider" which is a subclassed QAbstractSlider.

Within "myqslider" I have a slot "timeout()" which is meant to be called whenever the timer times out.

In the constructor for "myqslider" I have:


QTimer* internal_timer = new QTimer(this);
QObject::connect(internal_timer,SIGNAL(timeout()), this,SLOT(timeout()));


Whenever I run my application I get the following:

QObject: Cannot create children for a parent that is in a different thread. (Parent is myqslider(0x3966f0), parent's thread is QThread(0xb35b30), current thread is QThread(0x3969e0).

I am completely stumped. As far as I can tell every QObject has its own thread, and pretty much everything in Qt is based off of QObject so I dont think its a case of it being too "low in the chain" (i've tried putting it in the constructor for "control" anyway).

Does anyone have any ideas?

Thanks!

caduel
30th April 2009, 19:06
see the thread above yours
http://www.qtcentre.org/forum/f-qt-programming-2/t-qthread-and-qtimer-20639.html

sebastian.f
1st May 2009, 08:54
Hi caduel, thanks for your reply.

Please bear with me im not sure im understanding this right:

So say I have QObject1 in Thread 1, and this object instantiates another object (say a GUI control), this second object, QObject2, belongs to Thread1 although it controls Thread2.

Therefore when I created my timer in QObject2, it was being created in Thread1, but I was trying to control it from Thread2?

If this is correct, why am I still unable to at least create the timer in the constructor, surely it would be created (in Thread2) and I just wouldnt be able to interact with it?

caduel
1st May 2009, 10:45
when you subclass QThread (and add some member objects to the subclass) then both the QThread and the stuff you added in the subclass all belong to the thread you created your QThread-subclass-object in (the gui/main thread usually).

So, if you try to access them from the 'your' thread, you try to access objects belonging to another thread, which you should not do.

So:
do put those variables into a separate class which you create inside the run method of the thread
(or change the thread with QObject::moveToThread())

HTH

sebastian.f
1st May 2009, 11:00
Hi caduel. I changed the thread of my second object to that of the main application in the constructor and its working brilliantly now.

Thank you very much, I think i've got it now!