QThread emits finished() signal but isRunning() returns true and isFinished() returns false(QThread 发出 finished() 信号,但 isRunning() 返回 true 并且 isFinished() 返回 false)
问题描述
下面是我的 qthread 实现的代码.我正在尝试从卫星获取 gps 数据.即使程序退出 gpsSearch()
槽函数,QThread 也不会产生 finished() 信号.每当单击按钮时,都会调用函数 locateMe()
.第一次未启动线程并单击按钮时,它会为 isRunning()
函数打印真值,并为 isFinished()
函数打印假值.我不得不调用 QTherad 的 quit()
函数来手动停止线程.之后,它转到 gnssProvider
类中已连接的 threadQuit()
函数.但即使在那之后,如果我单击该按钮,它也会在 locateMe()
函数中打印 isRunning
的真值和 isFinished()
的假值.
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();
}
推荐答案
QThread生命周期的工作方式是这样的:
The way the QThread lifecycle works is like this:
- 你调用
QThread::start()
. - 此时,
isRunning()
应该开始返回 true. - 线程内部开始.它们发出
started()
信号. - 线程内部调用
run()
. - 除非您在子类中覆盖它,否则
run()
会调用exec()
. exec()
进入一个事件循环并停留在那里直到quit()
或exit()
被调用.exec()
和run()
返回内部.- 此时,
isFinished()
应该开始返回 true 和isRunning()
false. - 内部发出
finished()
信号. - 内部进行一些最后的清理工作.
- 线程真正终止了.
- You call
QThread::start()
. - At this point,
isRunning()
should start returning true. - The thread internals start. They emit the
started()
signal. - The thread internals call
run()
. - Unless you override this in a subclass,
run()
callsexec()
. exec()
enters an event loop and stays there untilquit()
orexit()
is called.exec()
andrun()
return to the internals.- At this point,
isFinished()
should start returning true andisRunning()
false. - The internals emit the
finished()
signal. - The internals do some final cleanups.
- The thread terminates for real.
所以你需要在你的位置获取器完成后调用 quit()
- 但是 this->quit()
没有调用 quit()
上线程!这可能就是它什么也没做的原因.
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/
注意她如何给她的工人一个 finished()
信号(与 QThread::finished
不同)并将其连接到 QThread::quit()
插槽.
Note how she gives her worker a finished()
signal (not the same as QThread::finished
) and connects it to the QThread::quit()
slot.
这篇关于QThread 发出 finished() 信号,但 isRunning() 返回 true 并且 isFinished() 返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QThread 发出 finished() 信号,但 isRunning() 返回 tru
基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 设计字符串本地化的最佳方法 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01