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
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.
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.
The way the QThread lifecycle works is like this:
QThread::start()
.isRunning()
should start returning true.started()
signal.run()
.run()
calls exec()
.exec()
enters an event loop and stays there until quit()
or exit()
is called.exec()
and run()
return to the internals.isFinished()
should start returning true and isRunning()
false.finished()
signal.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.