+ Reply to Thread
Results 1 to 2 of 2

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

  1. #1
    Join Date
    Feb 2015
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    Hi
    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
    4.  
    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. }
    11.  
    12. signals:
    13. void resultReady(const QString &result);
    14. };
    15.  
    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
    Location
    Munich, Germany
    Posts
    4,642
    Thanks
    21
    Thanked 407 Times in 401 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    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!):
    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.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

+ Reply to Thread

Similar Threads

  1. QThread::isRunning returns true after emitting finished signal
    By mentalmushroom in forum Qt Programming
    Replies: 10
    Last Post: 1st August 2012, 07:48
  2. clear emited signal queue
    By bibhukalyana in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2011, 11:20
  3. Replies: 3
    Last Post: 7th April 2011, 12:09
  4. Replies: 2
    Last Post: 20th March 2010, 19: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, 09:51

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