问题描述
所以我有一个工作类有2个插槽:StartWork()和StopWork(),StartWork()运行一个无限循环(它只读取和读取摄像机输入不停止)和StopWork()方法设置bool变量为false(所以StartWork()内的循环停止)。
根据QThread文档,现在使用它们的最好方法不是分类,但通过移动工人进入线程,所以这就是我做的。问题是,来自线程的starts()信号被调用,但是finished()信号从未被调用。
工作类插槽:
QThread初始化和信号/槽连接:
thread = new QThread();
worker = new Worker();
worker.moveToThread(thread);
QObject :: connect(thread,SIGNAL(started()),worker,SLOT(StartWork()));
QObject :: connect(thread,SIGNAL(finished()),worker,SLOT(StopWork()));
并且在我的QPushButton我这样做:
if(pushStartStop-> text()。toLower()==start)
{
pushStartStop-> setText(Stop);
thread-> start();
}
else
{
pushStartStop-> setText(Start);
thread-> quit();
}
线程 - > start()工作正常,StartWork调用和一切都很漂亮(GUI运行没有块等)。
但线程 - > quit()不做任何事情,改变文本),但是它。如果我只是调用worker-> StopWork()它的工作,但是我不能再次启动它。
我试过了thread-> exit()但结果是一样的。我也知道子类化工作,但它看起来更丑陋,根据最近的Qt文档,子类不再是最佳。
提前感谢。 >
您有一个永远的循环:
void StartWork()
{
running = true;
while(running)
{
do work;
}
}
此函数将循环,因此事件循环线程在发出 started()
后被阻塞。因此, finished()
无法发出。
解决方案1: >
添加函数
void DoWork()
{
if(running)
{
do work
}
}
b $ b
To Worker,并更改
void StartWork()
{
running =真正;
}
然后,只需连接 DoWork
更改函数
void StartWork()
{
running = true;
while(running)
{
do work;
QCoreApplication :: processEvents();
}
}
使用此解决方案,您必须重新启动线程当工作程序停止其工作(
StopWork()
将强制此函数完成,因此事件循环将没有事件要处理,线程将完成)。
so i have a worker class that has 2 slots: StartWork() and StopWork(), the StartWork() one runs an infinite loop (it just reads and reads camera input non-stop) and the StopWork() method just sets a bool variable to false (so the loop inside StartWork() stops).
according to the QThread documentation, the best way to use them now is not by sub-classing but by moving workers into the thread, so that's what i do. problem is, the started() signal from the thread gets called but the finished() signal never gets called.
worker class slots:
QThread initialization and signal/slot connection:
thread = new QThread();
worker = new Worker();
worker.moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(StartWork()));
QObject::connect(thread, SIGNAL(finished()), worker, SLOT(StopWork()));
and on my QPushButton i do this:
if(pushStartStop->text().toLower() == "start")
{
pushStartStop->setText("Stop");
thread->start();
}
else
{
pushStartStop->setText("Start");
thread->quit();
}
the thread->start() works fine, and the StartWork() gets called and everything is beautiful (GUI runs with no blocks, etc).
but thread->quit() doesn't do anything, it gets called (because the button changes text) but thats it. if i just call worker->StopWork() it works, but then i can't start it again.
I've tried with thread->exit(); but the results are the same. Also i know sub-classing works, but it looks uglier and according to the recent Qt documentation, sub-classing is no longer optimal.
thanks in advance.
解决方案
You have a forever loop:
void StartWork()
{
running = true;
while(running)
{
do work;
}
}
This function will loop, so the event loop of the thread is blocked just after having emitted
started()
. Thus, finished()
cannot be emitted.
Solution 1:
Add the function
void DoWork()
{
if(running)
{
do work
}
}
To Worker, and change
void StartWork()
{
running = true;
}
Then, just connect
DoWork
to a timer in the thread.
Solution 2:
Change the function
void StartWork()
{
running = true;
while(running)
{
do work;
QCoreApplication::processEvents();
}
}
With this solution, you will have to restart the thread when the worker stops its job (
StopWork()
will force this function to finish, so the event loop will have no events to handle and the thread will be finished).
这篇关于QThread完成()信号从不发出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!