QThread emits finished() signal but isRunning() returns true and isFinished() returns false

后端 未结 3 626
面向向阳花
面向向阳花 2020-12-05 09:02

Below is the code for my qthread implementation. I am trying to get gps data from satellite. QThread doesn\'t produce the finished() signal even when the programs exits

相关标签:
3条回答
  • 2020-12-05 09:05

    It's intended behavior that the thread doesn't quit until you manually terminate it. The thread object hosts an event loop, so it doesn't Finish until the event loop quits, as Sebastian explained.

    In short, your signal-slot connections are backwards conceptually - the object should terminate the thread when it is finished doing its thing, not the other way around.

    0 讨论(0)
  • 2020-12-05 09:11

    Which Qt version do you use?

    Qt 4.8 returned wrong values until 4.8.4 (Qt bug 30251). This bug has been fixed in 4.8.5.

    0 讨论(0)
  • 2020-12-05 09:14

    The way the QThread lifecycle works is like this:

    1. You call QThread::start().
    2. At this point, isRunning() should start returning true.
    3. The thread internals start. They emit the started() signal.
    4. The thread internals call run().
    5. Unless you override this in a subclass, run() calls exec().
    6. exec() enters an event loop and stays there until quit() or exit() is called.
    7. exec() and run() return to the internals.
    8. At this point, isFinished() should start returning true and isRunning() false.
    9. The internals emit the finished() signal.
    10. The internals do some final cleanups.
    11. The thread terminates for real.

    So you need to call quit() after your location fetcher is done - but this->quit() isn't calling quit() on the thread! This is probably why it's not doing anything.

    Your code looks a bit like it was patterned after this article:

    http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

    Note how she gives her worker a finished() signal (not the same as QThread::finished) and connects it to the QThread::quit() slot.

    0 讨论(0)
提交回复
热议问题