Results 1 to 4 of 4

Thread: QThread doesn't emit finished() signal when quit() or exit() slots are called

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread doesn't emit finished() signal when quit() or exit() slots are called

    I have MyService (derived from QtService<QCoreApplication>) which has a MyThread (derived from QThread). MyThread::run() creates a QTcpServer and called exec(). I'm on Windows 7 using Qt 4.6.2.

    In MyService::start() I call MyThread::start(). QThread::started() is emitted by MyThread.

    In MyService::stop() I call MyThread::quit(). I would expect QThread::finished() to be emitted by MyThread, but it isn't.

    If I call MyThread::terminate() from MyService::stop(), I get both QThread:finished() and QThread:terminated().

    Should I be expecting QThread:finished() to be emitted? The documentation says the following:

    void QThread::finished () [signal]
    This signal is emitted when the thread has finished executing.

    See also started() and terminated().
    Maybe the thread only emits QThread::finished() if it finishes of its own accord, i.e. not by having its quit() or exit() functions called.


    Added after 10 minutes:


    Interesting...

    I have just commented out the call to exec() from MyThread::run(). MyThread::run() returns straight away (it doesn't do anything now) and QThread::finished() is emitted.

    I take from this that one should not expect to get QThread::finished() when calling QThread::quit() or QThread::exit(). QThread::finished() is only emitted if the thread finishes of its own accord or QThread::terminate() is called. If QThread::terminate() is called, both QThread::finished() and QThread::terminated() are emitted.

    This does make sense. If you're calling QThread::quit() or QThread::exit() explicitly, you're assuming control and shouldn't need the thread to tell you that it's finished because you're telling it to finish.

    Anyone got anything to add?
    Last edited by stefanadelbert; 6th April 2011 at 05:38.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread doesn't emit finished() signal when quit() or exit() slots are called

    Yes, I have something to add - you're wrong, the signal gets emitted.

    Qt Code:
    1. #include <QtCore>
    2.  
    3. class Test : public QObject {
    4. Q_OBJECT
    5. public:
    6. Test(){}
    7. public slots:
    8. void onFinished() {
    9. qDebug() << Q_FUNC_INFO;
    10. }
    11. };
    12.  
    13. #include "main.moc"
    14.  
    15. int main(int argc, char **argv) {
    16. QCoreApplication app(argc, argv);
    17. QThread thread;
    18. Test test;
    19. QObject::connect(&thread, SIGNAL(finished()), &test, SLOT(onFinished()));
    20. thread.start(); // calls "exec()"
    21. QTimer::singleShot(1000, &thread, SLOT(quit()));
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    ./tth
    void Test::onFinished()
    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.


  3. #3
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread doesn't emit finished() signal when quit() or exit() slots are called

    Wysota, in my case QThread::finished() is not being emitted when QThread::quit() is called from QtService::stop(). Or more specifically, the slot I have connected to QThread::finished() is not being called if I call QThread::quit() from QtService::stop(). That doesn't mean that the signal isn't being emitted by QThread. Maybe something else is causing this signal not to be handled. Maybe the signal is on a queue somewhere and being handled too late.

    I tried your example and I can confirm that in that case the QThread::finished() IS being emitted, as expected. Your example is simpler that mine in that you don't deal with a QtService.

    I tried replacing MyThread with a QThread in my example, but I still get the same behaviour - QThread::finished() is not emitted when QThread::quit() is called from QtService::stop().

    However, if I set a timer (as in your example) to call QThread::quit() rather than call it from QtService::stop(), QThread::finished() is emitted and handled correctly!

    Anyone for anything to add?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread doesn't emit finished() signal when quit() or exit() slots are called

    Quote Originally Posted by stefanadelbert View Post
    Wysota, in my case QThread::finished() is not being emitted when QThread::quit() is called from QtService::stop().
    QThread doesn't care whether quit() is called from QtService or anything else. The signal is emitted.

    Or more specifically, the slot I have connected to QThread::finished() is not being called if I call QThread::quit() from QtService::stop().
    That might indeed be true.
    That doesn't mean that the signal isn't being emitted by QThread.
    Well... that's what you said previously - that QThread doesn't emit finished() when quit() is called.

    Maybe the signal is on a queue somewhere and being handled too late.
    If you quit the application right away then that's indeed the case. The signal is queued and will be delivered when the calling thread processes its events.
    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. Replies: 2
    Last Post: 1st December 2010, 11:33
  2. QThread quit() issue.
    By rokkamraja in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 16:34
  3. QThread crashes on exit
    By mhoover in forum Qt Programming
    Replies: 8
    Last Post: 26th August 2009, 20:19
  4. QUrlOperator doesn't emit finished signal
    By hayati in forum Qt Programming
    Replies: 16
    Last Post: 26th March 2007, 20:25
  5. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

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.