Results 1 to 5 of 5

Thread: Attempt to create QTimer results in: Cannot create children for a parent that is...

  1. #1
    Join Date
    Apr 2009
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Attempt to create QTimer results in: Cannot create children for a parent that is...

    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:
    Qt Code:
    1. QTimer* internal_timer = new QTimer(this);
    2. QObject::connect(internal_timer,SIGNAL(timeout()),this,SLOT(timeout()));
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Attempt to create QTimer results in: Cannot create children for a parent that is.


  3. #3
    Join Date
    Apr 2009
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Attempt to create QTimer results in: Cannot create children for a parent that is.

    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?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Attempt to create QTimer results in: Cannot create children for a parent that is.

    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

  5. The following user says thank you to caduel for this useful post:

    sebastian.f (1st May 2009)

  6. #5
    Join Date
    Apr 2009
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Attempt to create QTimer results in: Cannot create children for a parent that is.

    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.