Results 1 to 4 of 4

Thread: Executing slots in a separate QThread?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Executing slots in a separate QThread?

    Quote Originally Posted by kachofool View Post
    I have a quick question regarding the start up of a new thread, separate to the main thread, and executing a slot in it, from a signal invoked from the main GUI thread. I understand this behaviour is not normally recommended.
    It's fine.

    I just wanted to know if using moveToThread is the right way to do this (what if the thread quits or is terminated?)
    Moving the thread controller to the thread it controls is ok. If the thread quits, the object will probably go back under control of the thread that spawned the child thread in the first place (although that's just my assumption). If you want to do it another way then there is a very easy way to do it. In run() create an object of a new class that will contain the slot you want executed (instead of placing the slot where you have it now - in the thread class).

    Here is an example:
    Qt Code:
    1. class SolutionBringer : public QObject {
    2. Q_OBJECT
    3. public:
    4. //...
    5. public slots:
    6. void MyThreadSlot() { ... }
    7. };
    8.  
    9. class Thread : public QThread {
    10. Q_OBJECT
    11. public:
    12. // ...
    13. void run() {
    14. SolutionBringer bringer;
    15. connect(this, SIGNAL(TriggerThread()), &bringer, SLOT(MyThreadSlot()));
    16. emit ready();
    17. exec();
    18. }
    19. signals:
    20. void TriggerThread();
    21. void ready();
    22. };
    23.  
    24. // ...
    25.  
    26. Thread *thread = new Thread(...);
    27. connect(obj, SIGNAL(TriggerThread()), thread, SIGNAL(TriggerThread()));
    28. connect(thread, SIGNAL(ready()), &loop, SLOT(quit()));
    29. thread->start();
    30. loop.exec(); // wait until the thread signals it started its work
    31. // ...
    To copy to clipboard, switch view to plain text mode 
    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.


  2. The following user says thank you to wysota for this useful post:

    Chuk (21st February 2010)

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why slots in QThread subclasses are unsafe?
    By AlphaWolf in forum Qt Programming
    Replies: 8
    Last Post: 30th May 2010, 15:39
  3. QTableWidget, QThread, signals and slots
    By kazek3018 in forum Newbie
    Replies: 4
    Last Post: 30th December 2008, 21:21
  4. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47

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
  •  
Qt is a trademark of The Qt Company.