本文介绍了通过QVector< float>通过信号/插槽从工作线程到主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在将QVector传递到线程之间时遇到一些麻烦.目前,我有一个主线程(GUI-Thread)和一个经常发出QVector数组的辅助线程.直接在向量内部发射数据之前看起来不错.接收者是主线程中的一个插槽,但是该插槽接收到的数据是乱码.

Currently I have some troubles passing a QVector between to threads. At the moment I have a main thread (GUI-Thread) and an worker thread that emits frequently QVector arrays. Directly before emitting the data inside the vector looks good. The receiver is a slot in the main thread but the Data received in by the slot is garbled.

这是我的代码的某些部分:

Here are some parts of my code:

发射辅助线程:

void Pipeline::process
{
    QVector<float> buffer(w * h * d);

    // filling the vector with RGB-Values

    emit this->pushBuffer(buffer, w, h, d);
}

主线程中信号和插槽的连接:

Connection of signal and slot in the main thread:

QObject::connect(this->_pipeline.data(), SIGNAL(pushBuffer(const QVector<float>, int, int, int)), this->ui->widgetFiltered, SLOT(setBuffer(const QVector<float>,int,int,int)));

主线程中的插槽:

void GLWidget::setBuffer(const QVector<float> buffer, int dataSizeX, int dataSizeY, int dataSizeZ)
{
    // at this point the contents inside 'buffer' is garbled
}

使用QObject的moveToThread和QVector<float>在主方法中通过qRegisterMetaType< QVector<float> >("QVector<float>");注册到元系统中来启动线程.

The Thread is started by using QObject's moveToThread and QVector<float> registered to the meta-system by qRegisterMetaType< QVector<float> >("QVector<float>"); in the main method.

Pipeline::process返回之后数据是否有可能丢失?在这种多线程情况下,我不确定QVector内部的隐式共享如何.

Is it possible that the data gets lost after Pipeline::process returns? I am not sure how the implicit sharing inside QVectorbehaves in this multi-threaded case.

任何帮助将不胜感激.

问候

推荐答案

a)注册元类型QVector.在主要功能的app.exec()之前添加以下行:

a) Register metatype QVector. Add this line before app.exec() in your main function:

qRegisterMetaType<QVector<float> >("QVector<float>");

没有此QueuedConnection将不起作用.

Without this QueuedConnection won't work.

b)明确表示您的信号和插槽通过Qt::QueuedConnection连接如果在连接后执行moveToThread,这应该可以在适当的线程中修复插槽的执行情况.

b) explicitly say that your signal and slot connected via Qt::QueuedConnectionif you do moveToThread after connect, this should fix slot execution in proper thread.

这篇关于通过QVector&lt; float&gt;通过信号/插槽从工作线程到主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 01:59