Results 1 to 8 of 8

Thread: QThread Signal Not Received By Main Thread Slot

  1. #1
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread Signal Not Received By Main Thread Slot

    I'm having a rather strange problem, and I'm wondering if anyone can help set me on track.

    I'm new to working with QThreads, and I'm quite sure that this is a logic problem more than anything else.

    I can't seem to connect my thread's custom signal to the desired slot on the main thread that launched it.

    "mythread.h"
    Qt Code:
    1. #include <QtGui>
    2. class MyThread: public QThread
    3. {
    4. Q_OBJECT
    5. public:
    6. MyThread(int thread_id)
    7. {
    8. connect(this, SIGNAL(threadFinished(int)), this, SLOT(finished(int)),Qt::DirectConnection);
    9. }
    10. ~MyThread();
    11.  
    12. signals:
    13. void threadFinished(int thread_id);
    14.  
    15. public slots:
    16. void finished(int)
    17. {
    18. qDebug() << "finished(int) received in thread"; // output displays this
    19. }
    20.  
    21. protected:
    22. void run()
    23. {
    24. runGeneralQuery();
    25. runProgramNameQuery();
    26.  
    27. emit threadFinished(thread_id);
    28. }
    29.  
    30. private:
    31. void runGeneralQuery(){};
    32. void runProgramNameQuery(){};
    33.  
    34. };
    To copy to clipboard, switch view to plain text mode 

    "threadmanager.h"
    Qt Code:
    1. #include <QtGui>
    2. #include "mythread.h"
    3.  
    4. class ThreadManager: public QObject
    5. {
    6. Q_OBJECT
    7. public:
    8. ThreadManager()
    9. {
    10. /*...*/
    11. runThreads();
    12. }
    13.  
    14.  
    15. void runThreads()
    16. {
    17. thread= new MyThread(0);
    18. connect(thread, SIGNAL(threadFinished(int)), this, SLOT(finished(int)), Qt::DirectConnection);
    19.  
    20. thread->start();
    21.  
    22. }
    23.  
    24.  
    25. public slots:
    26. void finished(int thread_id)
    27. {
    28. qDebug() << "finished(int) received in manager"; // output does not display this
    29. }
    30.  
    31. private:
    32. MyThread* thread;
    33.  
    34. };
    To copy to clipboard, switch view to plain text mode 

    Does anyone have any suggestions as to why the signal isn't being received by the slot on the main thread?

    Thanks in advance!
    Last edited by EF2008; 3rd June 2010 at 20:35. Reason: updated contents

  2. #2
    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: QThread Signal Not Received By Main Thread Slot

    What happens if you remove Qt::DirectConnection ?

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Signal Not Received By Main Thread Slot

    You can't use DirectConnection with threads, they must be queued.

  4. #4
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Signal Not Received By Main Thread Slot

    No change, unfortunately.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Signal Not Received By Main Thread Slot

    Where do you run the event loop for the thread?

  6. #6
    Join Date
    Jan 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Signal Not Received By Main Thread Slot

    Is executing the thread's event loop necessary for what I'm doing? I just need work to be done in MyThread::run(), have it emit a signal, and return...

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Signal Not Received By Main Thread Slot

    It shouldn't be necessary tbh, just a stab in the dark.

    What happens when you step through the run method?

  8. #8
    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: QThread Signal Not Received By Main Thread Slot

    Let's make one thing clear - you are not making connections across threads. QThread objects live in the main thread (at least in your situation).
    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. QThread slot executed in GUI thread
    By tnyblom in forum Qt Programming
    Replies: 13
    Last Post: 25th May 2010, 07:49
  2. Signal never received from network capture thread
    By skimpax in forum Qt Programming
    Replies: 9
    Last Post: 4th February 2010, 12:59
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Determine if QThread::currentThread is main thread?
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2008, 06:26
  5. Replies: 1
    Last Post: 11th September 2008, 20:45

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.