Passing QVector<float> from worker thread to main thread via signal/slot

元气小坏坏 提交于 2019-12-22 09:46:22

问题


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:

Emit in the worker thread:

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

Slot in the main thread:

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

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.

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.

Any help would be appreciated.

Greetings

Wolf


回答1:


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

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

Without this QueuedConnection won't work.

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



来源:https://stackoverflow.com/questions/10719553/passing-qvectorfloat-from-worker-thread-to-main-thread-via-signal-slot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!