Results 1 to 5 of 5

Thread: QObject::setParent across threads.

  1. #1
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QObject::setParent across threads.

    Hi

    I'm having some problems with the QObject::setParent() function across threads. I'm seeing this behavior on windows only. On mac it works fine. Qt 4.6.2 in used on both platforms.
    Here is the code:

    Qt Code:
    1. MyObject *object = new MyObject();
    2. if (parent) {
    3. if (parent->thread() != object->thread()) {
    4. object->moveToThread(parent->thread());
    5. }
    6. Q_ASSERT(parent->thread() == object->thread());
    7. object->setParent(parent);
    8. }
    To copy to clipboard, switch view to plain text mode 

    The error message is as follows:

    Cannot send events to objects owned by a different thread. Current thread 588d088. Receiver 'MyObject' (of type 'MyObject') was created in thread a4f690

    What could be wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::setParent across threads.

    QObject is not thread-safe, accessing its members from thread other than that which the object belongs to may lead to a crash. Furthermore objects can't have a parent that lives in a diffrent thread than they do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QObject::setParent across threads.

    that's why I call movetoThead before calling setParent.

    the docks mention that QObjects are not thread safe, but it does not mean that they can not be used across threads at all... Also the docs specifically warn the user when the given function is not thread safe.
    Which part exactly causes problems?
    1) object construction should work fine
    2) moveToThread should be thread safe - the object is pushed away from the thread, in which it has been created.
    3) setParent ??

    From the error message I'm guessing that moveToThread have not finished something?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::setParent across threads.

    Quote Originally Posted by psih128 View Post
    that's why I call movetoThead before calling setParent.
    You cannot "pull" an object from another thread to your own thread. Only the thread that owns the object can call moveToThread().

    the docks mention that QObjects are not thread safe, but it does not mean that they can not be used across threads at all...
    That's exactly what "not thread-safe" means - the same object can't be called concurrently from different threads.

    2) moveToThread should be thread safe - the object is pushed away from the thread, in which it has been created.
    See above

    3) setParent ??
    It's definitely not thread-safe
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2008
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QObject::setParent across threads.

    the statement " setParent(parent)" isn't working because "moveToThread" has finished its work perfectly

    - moveToThread() moved the object to another thread other than the current, so that you can not call setParent() of the object anymore.

    and the problem is that you can not setParent() to a parent that is not the current thread also ( before calling moveToThread) !

    have you solved this problem ?

Similar Threads

  1. Qt widget setParent
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 30th November 2007, 20:38
  2. Replies: 1
    Last Post: 31st October 2007, 14:14
  3. help on QObject::tr()
    By sincnarf in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 07:52
  4. Reparenting a QObject
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2007, 17:21
  5. Replies: 6
    Last Post: 6th December 2006, 22:44

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.