使用QtConcurrent :: run在单独的线程上连接信号/插槽 [英] Connecting signals/slots on separate thread using QtConcurrent::run
问题描述
在我的应用程序中,对话框中有以下代码:
In my application I have the following code in a dialog:
connect(drive, SIGNAL(FileProgressChanged(Progress)), SLOT(OnFileProgressChanged(Progress)));
QtConcurrent::run(this, &ProgressDialog::PerformOperation, Operation, *Path, OutPath, drive);
PerformOperation函数最终调用drive
中的一个函数,该函数发出信号FileProgressChanged
,而我的OnFileProgressChanged
函数如下:
The PerformOperation function eventually calls to a function in drive
which emits the signal FileProgressChanged
, and my OnFileProgressChanged
function is as follows:
void ProgressDialog::OnFileProgressChanged(Progress p)
{
if (ui->progressCurrent->maximum() != p.Maximium)
ui->progressCurrent->setMaximum(p.Maximium);
ui->progressCurrent->setValue(p.Current);
if (ui->groupBoxCurrent->title().toStdString() != p.FilePath)
ui->groupBoxCurrent->setTitle(QString::fromStdString(p.FilePath));
}
我正在阅读,发现 QFuture 和 QFutureWatcher 支持监视进度值(在这种情况下非常有用!),但是那些不能与QtConcurrent::run
一起使用.
I was doing some reading and saw that QFuture and QFutureWatcher support monitoring progress values (which would work great in this situation!), but those cannot be used in conjunction with QtConcurrent::run
.
我该如何将在单独线程上发出的移动信号连接到我的主线程上的插槽,以便我可以监视在发射器线程上调用的函数的进度?
How would I go about connecting the signal that gets moved emitted on the separate thread to the slot on my main thread so I can monitor the progress of the function called on the emitter thread?
* 编辑-* 我实际上发现我的代码有错误,但似乎没有影响.我忘记在信号后添加this
作为参数
*Edit -- * I actually found an error with my code, but it doesn't seem to have an affect. I forgot to add this
as an argument after the signal
connect(drive, SIGNAL(FileProgressChanged(Progress)), this, SLOT(OnFileProgressChanged(Progress)));
推荐答案
尝试将connect()
与QueuedConnection
结合使用,例如:
Try using connect()
with QueuedConnection
, like:
connect(drive, SIGNAL(FileProgressChanged(Progress)), this, SLOT(OnFileProgressChanged(Progress)), Qt::QueuedConnection);
默认情况下,连接应该已经在队列中(因为发射器和接收器处于不同的线程中),但这只会使其更加明确.
The connection should already be queued by default (since the emitter and receiver are in different threads), but this just makes it more explicit.
问题是Progress
类型没有在Qt的元对象系统中注册.添加qRegisterMetaType<Progress>("Progress");
可以解决此问题.
The problem was that the Progress
type wasn't registered with Qt's meta-object system. Adding qRegisterMetaType<Progress>("Progress");
fixed the problem.
这篇关于使用QtConcurrent :: run在单独的线程上连接信号/插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!