Tah*_*lil 10 c++ qt gps qthread blackberry-10
下面是我的qthread实现的代码.我试图从卫星获取gps数据.即使程序退出gpsSearch()插槽功能,QThread也不会产生finished()信号.locateMe()只要单击一个按钮,就会调用该函数.第一次没有启动线程并单击该按钮时,它会为isRunning()函数打印true值并为函数打印false值isFinished().我不得不调用quit()QTherad 的函数来手动停止线程.之后,它将转到类中的连接threadQuit()函数gnssProvider.但即便如此,如果我单击按钮,它会在函数中输出true isRunning和false .isFinished()locateMe()
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();
}
Run Code Online (Sandbox Code Playgroud)
Seb*_*edl 31
QThread生命周期的工作方式如下:
QThread::start().isRunning()应该开始返回true.started()信号.run().run()调用exec().exec()进入一个事件循环并停留在那里直到quit()或被exit()调用.exec()然后run()回到内部.isFinished()应该开始返回true和isRunning()false.finished()信号.所以你需要quit()在你的位置提取器完成后调用- 但是this->quit()没有调用quit()线程!这可能就是它没有做任何事情的原因.
您的代码看起来有点像本文后的图案:
http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
注意她如何给她的工人一个finished()信号(不一样QThread::finished)并将其连接到QThread::quit()插槽.