Results 1 to 2 of 2

Thread: Signal QThread::finished() is not emited!

  1. #1
    Join Date
    Feb 2015
    Thanked 1 Time in 1 Post
    Qt products
    Unix/X11 Windows Android

    Exclamation Signal QThread::finished() is not emited!

    I wrote simple Sqllite database app (automatic update to newer version). The only unusual thing in this app is that main thread is used for display progress bars and log output, and second thread exec update code.
    Currently application works as expected but one thing does not work. After properly execution main thread is not informed about second thread finish!
    My thread concept usage is as follow (example from Qt doc):
    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT
    5. public slots:
    6. void doWork(const QString &parameter) {
    7. QString result;
    8. /* ... here is the expensive or blocking operation ... */
    9. emit resultReady(result);
    10. }
    12. signals:
    13. void resultReady(const QString &result);
    14. };
    16. class Controller : public QObject
    17. {
    18. Q_OBJECT
    19. QThread workerThread;
    20. public:
    21. Controller() {
    22. Worker *worker = new Worker;
    23. worker->moveToThread(&workerThread);
    24. connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
    25. connect(this, &Controller::operate, worker, &Worker::doWork);
    26. connect(worker, &Worker::resultReady, this, &Controller::handleResults);
    27. workerThread.start();
    28. }
    29. ~Controller() {
    30. workerThread.quit();
    31. workerThread.wait();
    32. }
    33. public slots:
    34. void handleResults(const QString &);
    35. signals:
    36. void operate(const QString &);
    37. };
    To copy to clipboard, switch view to plain text mode 

    With exception that after workerThread.start(); I emit signal which call slot in the second thread (this is working task). After my operations I expect that QThread::finished() will be emitted by the second thread. But nothing happen like that...

    So my questions are as follow:
    1) do I something wrong?
    2) Should I emit some custom signal from second thread to inform main thread about end of processing in the second thread?!?

    thanks and best wishes
    Szyk Cech

  2. #2
    Join Date
    Jan 2006
    Munich, Germany
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows

    Default Re: Signal QThread::finished() is not emited!

    After my operations I expect that QThread::finished() will be emitted by the second thread. But nothing happen like that...
    How do you know that?
    The QThread::finished() signal is connected to your Workers object deleteLater - so your code is not called by it - which is why I ask how do you know its not being fired?
    One way to test would be to create a new slot in your Controller that also connects to QThread::finished() where you can have an output or put a breakpoint in it while running in a debugger.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):

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

Similar Threads

  1. QThread::isRunning returns true after emitting finished signal
    By mentalmushroom in forum Qt Programming
    Replies: 10
    Last Post: 1st August 2012, 06:48
  2. clear emited signal queue
    By bibhukalyana in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2011, 10:20
  3. Replies: 3
    Last Post: 7th April 2011, 11:09
  4. Replies: 2
    Last Post: 20th March 2010, 18:22
  5. how to check if a signal in emited or not??
    By sudhansu in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 08:51


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.