Results 1 to 8 of 8

Thread: QThread Signal Not Received By Main Thread Slot

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.