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

后端 未结 3 639
面向向阳花
面向向阳花 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: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.

提交回复
热议问题