Results 1 to 9 of 9

Thread: Moving QObject to QThread causes signals to stop working

  1. #1
    Join Date
    May 2009
    Posts
    20
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Moving QObject to QThread causes signals to stop working

    I know that QThreads are a tricky issue for most. I've read through a lot of examples and documentation to setup my threading correctly under QT. The 2 methods seem to be sub-classing QThread (which although the documentation says to do this, it is apparently the incorrect way) and the other is subclassing QObject and then moving it to a thread. Trying both methods I was unable to get a simple signal in the thread to connect to a slot in my main thread.

    For the first method if I did not use moveToThread(this) in the subclassed QThread constructor, it would not execute in a new thread, but the signals worked. Using moveToThread(this) caused the connection to be broken in some way.

    Trying it the 2nd method, my signals in the worker object worked until I moved them to a QThread.

    Are there any known issues with using QThread under QT 4.6.3 and compiling with MSVC 2010 on Win7 x64( the app is compiled as 32-bit)?

    The signal I'm emitting is a QString (declare as: void StringSignal(QString&);, used as: emit StringSignal(QString("Hello"))
    In the first method the connection is made as:
    Qt Code:
    1. connect(&thread,SIGNAL(StringSignal(QString&)),this,SLOT(SignalSlot(QString&)));)
    To copy to clipboard, switch view to plain text mode 

    My code isn't any different than the Mandelbrot example (for the first method I tried). I call a function in the QThread that sets some values and either starts the thread if it isn't running or wakes the QWaitCondition.
    Last edited by Ban-chan; 13th July 2010 at 16:55.

  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: Moving QObject to QThread causes signals to stop working

    Does the thread run an event loop?
    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
    May 2009
    Posts
    20
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving QObject to QThread causes signals to stop working

    As in do I call exec() in the run function? I do not. But they don't in the Mandelbrot example either and it works just fine.

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

    Default Re: Moving QObject to QThread causes signals to stop working

    The mandelbrot example isn't a good one, in fact it isn't even correct although it works.

    QThread changed around Qt4.2 or 4.3 to start an event loop when starting a thread. The problem is that when you subclass QThread and implement the run() function, the automatic event loop is lost and you need to start it yourself.

    That's why it is recommended not to subclass QThread at all and use either Qt Concurrent classes or create a QObject and move it to a thread.

    Can you post the code where you created a QObject subclass and moved it to a thread?

  5. #5
    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: Moving QObject to QThread causes signals to stop working

    Quote Originally Posted by Ban-chan View Post
    As in do I call exec() in the run function? I do not.
    Then you don't get to be notified about signals from other threads.

    But they don't in the Mandelbrot example either and it works just fine.
    Oh, I'm sure the thread where slots are executed (which happens to be the main thread in this case) has an event loop running.
    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.


  6. #6
    Join Date
    May 2009
    Posts
    20
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving QObject to QThread causes signals to stop working

    Quote Originally Posted by wysota View Post
    Then you don't get to be notified about signals from other threads.
    That's fine I'm just emiting Signals from the thread. I don't have any slots in it.


    Oh, I'm sure the thread where slots are executed (which happens to be the main thread in this case) has an event loop running.
    Yes, as does my main thread (GUI thread).

  7. #7
    Join Date
    May 2009
    Posts
    20
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving QObject to QThread causes signals to stop working

    Quote Originally Posted by tbscope View Post
    The mandelbrot example isn't a good one, in fact it isn't even correct although it works.

    QThread changed around Qt4.2 or 4.3 to start an event loop when starting a thread. The problem is that when you subclass QThread and implement the run() function, the automatic event loop is lost and you need to start it yourself.

    That's why it is recommended not to subclass QThread at all and use either Qt Concurrent classes or create a QObject and move it to a thread.

    Can you post the code where you created a QObject subclass and moved it to a thread?
    I have a few questions to make sure I'm doing this method right in the first place.
    Is the proper method of doing this to setup"tasks" in the QObject as slots that I call from my main thread (GUI thread)? The exec() default implementation would ensure I have an event loop in the QThread I move it too, correct? Or should I link the started() signal from the QThread to a slot in my QObject that uses a forever loop and QWaitConditions, similar to what is shown in the Mandelbrot example?

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

    Default Re: Moving QObject to QThread causes signals to stop working

    Quote Originally Posted by Ban-chan View Post
    I have a few questions to make sure I'm doing this method right in the first place.
    Is the proper method of doing this to setup"tasks" in the QObject as slots that I call from my main thread (GUI thread)?
    I'm not sure what you mean exactly. But I guess yes, you create a QObject based class just like any other. As an example, you can consider a class for calculating prime numbers in a certain range. You can create a function or even a slot to set the range (and link this slot to a slider for example). And create a slot to start calculating etc...

    The exec() default implementation would ensure I have an event loop in the QThread I move it too, correct?
    Yes, QThread by default takes care of the event loop.

    Or should I link the started() signal from the QThread to a slot in my QObject that uses a forever loop and QWaitConditions, similar to what is shown in the Mandelbrot example?
    No, you shouldn't need to do that since the event loop (started with exec() ) runs till you call quit().

    Here's an example:
    http://labs.trolltech.com/blogs/2006...-the-headache/

  9. #9
    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: Moving QObject to QThread causes signals to stop working

    I would like to protest against a blind argument that subclassing QThread doesn't make sense anymore. Let's consider the prime number situation. It's much easier to do this:
    Qt Code:
    1. class PrimeThread : public QThread {
    2. Q_OBJECT
    3. public:
    4. void run() { for(int i=1;i<1000;i++) if(isPrime(i)) emit prime(i); }
    5. signals:
    6. void prime(int);
    7. //...
    8. };
    9.  
    10. PrimeThread thread;
    11. thread.start();
    To copy to clipboard, switch view to plain text mode 
    than this:
    Qt Code:
    1. class PrimeObject : public QObject {
    2. Q_OBJECT
    3. public slots:
    4. void run() {
    5. for(int i=1;i<1000;i++) if(isPrime(i)) emit prime(i);
    6. emit quit();
    7. }
    8. signals:
    9. void prime(int);
    10. void quit();
    11. };
    12.  
    13. QThread thread;
    14. thread.start();
    15. PrimeObject o;
    16. connect(&o, SIGNAL(quit()), &thread, SLOT(quit()));
    17. o.moveToThread(&thread);
    18. QMetaObject::invokeMethod(&o, "run", Qt::QueuedConnection);
    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.


Similar Threads

  1. How to stop QThread?
    By vespasianvs in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2010, 06:42
  2. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 13:33
  3. Replies: 1
    Last Post: 10th October 2007, 10:11
  4. Stop window moving when clicking LMB
    By steg90 in forum Qt Programming
    Replies: 11
    Last Post: 11th June 2007, 10:59
  5. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51

Tags for this Thread

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.