Results 1 to 2 of 2

Thread: MoveToThread clarification

  1. #1
    Join Date
    Sep 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default MoveToThread clarification

    I read some article on the Qt dev blog which suggest the proper way to do multithread stuff is to use moveToThread instead of subclassing QThread.

    I have this class that does some serious work which i like to run on a separate thread.

    So basically the code look something like this:

    Qt Code:
    1. .H file
    2. class Data : QObject
    3. {
    4. public:
    5. void update();
    6. };
    7.  
    8. class Worker : QObject
    9. {
    10. public:
    11. void setData( Data* data ) { mData = data; }
    12.  
    13. signals:
    14. void workCompleted();
    15.  
    16. public slots:
    17. void doWork();
    18.  
    19. private:
    20. Data* mData;
    21. };
    22. class Master : QObject
    23. {
    24. Master() { mWorker = new Worker; mWorkerThread = new QThread; }
    25. void askWorkerToWork();
    26.  
    27. private:
    28. Worker* mWorker;
    29. QThread* mWorkerThread;
    30. };
    31.  
    32. .CPP file
    33. void Data::update()
    34. {
    35. UpdateEvent e("Updated");
    36. QCoreApplication::sendEvent(this, &e);
    37. }
    38.  
    39. void Worker::doWork()
    40. {
    41. // run some computation
    42. this->moveToThread(QCoreApplication::instance()->thread());
    43. mData->update();
    44. emit workCompleted();
    45. }
    46.  
    47. void Master::askWorkerToWork()
    48. {
    49. Data* data = new Data;
    50. mWorker->setData(data);
    51. mWorker->moveToThread(mWorkerThread);
    52. connect(mWorkerThread, SIGNAL(started()), mWorker, SLOT(doWork()), Qt::UniqueConnection);
    53. connect(mWorker, SIGNAL(workCompleted()), mWorkerThread, SLOT(quit()), Qt::UniqueConnection);
    54. mWorkerThread->start();
    55. }
    To copy to clipboard, switch view to plain text mode 

    I would like to know given the above setup does Qt automatically move the mWorker back to thread which Master object is created when the thread quits? Or do I have to do a moveToThread as shown in the code manually?

    Does the moveToThread function actually move the object to the target thread immediately? Qt complains that I cannot call the function QCoreApplication::sendEvent in the Data object because it is created on a different thread.

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

    Default Re: MoveToThread clarification

    Qt doesn't move any objects anywhere on its own. Using sendEvent() on an object that lives in a different thread doesn't make any sense. Use postEvent() instead.
    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.


Similar Threads

  1. QMovie::moveToThread() and its QTimer
    By tiftof in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2011, 10:53
  2. QThread clarification
    By janorcutt in forum Qt Programming
    Replies: 5
    Last Post: 6th January 2011, 09:11
  3. Clarification about dataChanged
    By airbites in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 16:02
  4. moveToThread() Problem
    By rangerbob in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:33
  5. QObject::moveToThread() warnings
    By gri in forum Qt Programming
    Replies: 9
    Last Post: 2nd April 2007, 16:46

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.