4
votes

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

1 Answers

5
votes

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.