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 gpsSearch()
slot function. The function locateMe()
is called whenever a button is clicked. The first time when the thread isnt started and the button is clicked it prints true value for isRunning()
function and prints false value for isFinished()
function. I had to call the quit()
function of the QTherad to manually stop the thread. After that it goes to the connected threadQuit()
function in the gnssProvider
class. But even after that if I click the button it prints true value for isRunning
and false for isFinished()
in the locateMe()
function.
GPSInfo::GPSInfo()
{
hybridGPSFound = satelliteGPSFound = networkGPSFound = false;
qDebug()<<"Thread Creating";
gnssThread = new QThread;
gnssProvider = new LocationFetcher(this,GEOLOCATION_PROVIDER_GNSS,1);
gnssProvider->moveToThread(gnssThread);
connect(gnssThread, SIGNAL(started()), gnssProvider, SLOT(gpsSearch()));
connect(gnssThread, SIGNAL(finished()), gnssProvider, SLOT(threadQuit()));
}
void LocationFetcher::gpsSearch()
{
if (BPS_SUCCESS != geolocation_request_events(0))
{
fprintf(stderr, "Error requesting geolocation events: %s", strerror(errno));
return;
}
geolocation_set_provider(GPS_Search_Provider);
geolocation_set_period(GPS_Search_Period);
while (!stopThread)
{
bps_event_t *event = NULL;
bps_get_event(&event, -1);
if (event)
{
if (bps_event_get_domain(event) == geolocation_get_domain() && bps_event_get_code(event) == GEOLOCATION_INFO)
{
handle_geolocation_response(event);
break;
}
}
}
geolocation_stop_events(0);
this->quit();
}
void GPSInfo::LocateMe()
{
qDebug()<<"Thread Running: "<<gnssThread->isFinished();
qDebug()<<"Thread Running: "<<gnssThread->isRunning();
gnssThread->start();
hybridThread->start();
networkThread->start();
}
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.