Results 1 to 11 of 11

Thread: Receiving slots in QThread's context ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Receiving slots in QThread's context ?

    Quote Originally Posted by The Storm View Post
    I just wonder why it can't be done with my QThread subclass.
    The conecpt of QThread is not easy to understand by a lot of people, especially because the official documentation of QThread is not sufficient or even correct.
    During the lifetime of Qt4 QThread changed, but the documentation remained the same.

    Why do signals and slots in a QThread subclass not work as expected?
    The only code that runs in a thread other than the main thread is the code that is written in the run() function, or that is being called from the run() function.

    However, you define signal and slot functions in the QThread subclass. If you create a QThread object in your program, this QThread object lives in the main thread. Therefor the signals and slots are also define in the main thread.

    Like I said above, when you call a function of QThread or a subclass from within the run() function, it will also be performed in the new thread. No problem for signals.
    Slots are much more complicated.

    If for example you connect a slot in your QThread subclass in the constructor of your QThread subclass, this slot will be called in the main thread!

    Some people use moveToThread from within their QThread subclass. Although this SEEMS to work, it is wrong. You want to have the QThread object live in your main thread.
    Therefor it is suggested to create a QObject subclass, implement it like any other object. Then create a new QThread object and move the QObject object to the new QThread object. In the background, the whole QObject object is moved to a new thread, instantly executing the slots inside the new thread.

    I guess you can compare it like this:
    Qt Code:
    1. class MyThread: public QThread
    2. {
    3. ...
    4. //slots
    5. //signals
    6. ...
    7. };
    To copy to clipboard, switch view to plain text mode 

    to
    Qt Code:
    1. class MyObjectThread : public
    2. {
    3. public:
    4. void run();
    5. };
    6.  
    7. void MyObjectThread::run()
    8. {
    9. MyObject *obj = new MyObject;
    10.  
    11. connect(..., SIGNAL, obj, SLOT);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Since the object is created in the run() function, it completely lives in the new thread.

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

    The Storm (14th August 2010)

Similar Threads

  1. Problem with Signal and Slots and QThread
    By Informatix in forum Newbie
    Replies: 2
    Last Post: 7th June 2010, 15:21
  2. QThread signal and slots problem
    By mero in forum Qt Programming
    Replies: 8
    Last Post: 8th May 2010, 02:56
  3. Any examples of receiving slots in threads?
    By draftpunk in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2010, 20:21
  4. QTableWidget, QThread, signals and slots
    By kazek3018 in forum Newbie
    Replies: 4
    Last Post: 30th December 2008, 22:21
  5. QThread communication without signal and slots
    By forrestfsu in forum Qt Programming
    Replies: 16
    Last Post: 22nd May 2007, 01:43

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.