QThread finished()信号永远不会发出(QThread finished() signal is never emited)

编程入门 行业动态 更新时间:2024-10-16 02:27:58
QThread finished()信号永远不会发出(QThread finished() signal is never emited)

所以我有一个有2个插槽的工作类:StartWork()和StopWork(),StartWork()运行一个无限循环(它只是读取和读取摄像头输入不停),StopWork()方法只是设置一个bool变量为false(因此StartWork()内的循环停止)。

根据QThread文档,现在使用它们的最佳方法不是通过子类化,而是通过将工作者移动到线程中,这就是我所做的。 问题是,来自线程的started()信号被调用,但完成的()信号永远不会被调用。

工人类槽:

void StartWork(){running = true; 而(跑步){做工; }}

void StopWork(){running = false; }

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(); }

thread-> start()工作正常,StartWork()被调用,一切都很美(GUI运行没有块等)。

但是thread-> quit()没有做任何事情,它被调用(因为按钮改变文本),但就是这样。 如果我只是调用worker-> StopWork()它可以工作,但是我不能再启动它。

我尝试过thread-> exit(); 但结果是一样的。 我也知道子类分类工作,但看起来更丑陋,根据最近的Qt文档,子分类不再是最佳的。

提前致谢。

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:

void StartWork(){ running = true; while(running){ do work; }}

void StopWork(){ running = false; }

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.

最满意答案

你有一个永远的循环:

void StartWork() { running = true; while(running) { do work; } }

这个函数将循环,因此线程的事件循环在发出started()之后就被阻塞了。 因此,无法发出finished() 。

解决方案1:

添加功能

void DoWork() { if(running) { do work } }

对工人而言,改变

void StartWork() { running = true; }

然后,只需将DoWork连接到线程中的计时器。

解决方案2:

改变功能

void StartWork() { running = true; while(running) { do work; QCoreApplication::processEvents(); } }

使用此解决方案,您将不得不在工作程序停止其作业时重新启动线程( StopWork()将强制此函数完成,因此事件循环将没有要处理的事件并且线程将完成)。

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).

更多推荐

本文发布于:2023-08-06 08:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   信号   finished   QThread   signal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!