Results 1 to 3 of 3

Thread: signals are not working from QThread derived class

  1. #1
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default signals are not working from QThread derived class

    Hi all,

    Application couldn't receive signal from QThread object.

    I have implemented this way:

    workerthread.h:
    ----------------------
    Qt Code:
    1. #ifndef WORKERTHREAD_H
    2. #define WORKERTHREAD_H
    3.  
    4. #include <QThread>
    5.  
    6. class workerthread : public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit workerthread(QObject *parent = 0);
    11. void run();
    12.  
    13. signals:
    14. void signal_worker_thread();
    15. };
    16.  
    17. #endif // WORKERTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    workerthread.cpp
    -------------------------
    Qt Code:
    1. #include "workerthread.h"
    2. #include <QDebug>
    3.  
    4. workerthread::workerthread(QObject *parent) :
    5. QThread(parent)
    6. {
    7. }
    8.  
    9. void workerthread::run()
    10. {
    11. for(int i = 0; i < 10; ++ i)
    12. {
    13. qDebug() << "workerthread::signal_worker_thread()";
    14. emit signal_worker_thread();
    15. }
    16. return;
    17. }
    To copy to clipboard, switch view to plain text mode 

    WorkerThread is derived from QThread and overridden Run() method.
    I have launched this thread on GUI - button click event to do background process.

    Button click slot event:
    ---------------------------------
    Qt Code:
    1. void ProductNameDialog::slotButtonClicked()//
    2. {
    3. workerthread *pthread = new workerthread();
    4. QObject::connect(pthread, SIGNAL(signal_worker_thread()), this, SLOT(slotTest()));
    5.  
    6. pthread->start();
    7. pthread->wait();
    8.  
    9. accept();
    10. return;
    11. }
    To copy to clipboard, switch view to plain text mode 


    I have tested with sample Qt-GUI MainWindow application. It's working fine.

    When I integrated into my application, slotTest() is not fired. I hope, I am missing something. Can somebody suggest me what I am missing here.

    Thanks in advance.
    Last edited by high_flyer; 16th September 2013 at 10:56. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: signals are not working from QThread derived class

    the problem is that you call QThread::wait()
    That will block the caller thread, in your case your GUI thread. While it is blocked it can't do anything, so it cannot process the signal event it is getting.

    Potential solution (depending in your problem)
    do not call wait, connect thread's finished() signal to dialog's accept() slot before calling thread's start method.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: signals are not working from QThread derived class

    in addition to what anda_skoa said, having the calling thread wait for your thread to end by blocking it, makes having the thread redundant.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Problems with signals and slots in derived class
    By ittod in forum Qt Programming
    Replies: 6
    Last Post: 21st May 2013, 22:16
  2. Signals not working in a QThread
    By curreli in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2010, 16:04
  3. Moving QObject to QThread causes signals to stop working
    By Ban-chan in forum Qt Programming
    Replies: 8
    Last Post: 13th July 2010, 21:39
  4. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.