Hi,
so, we have now a "best practice" approach when dealing with shallow-copy Qt objects being passed between threads.
First, create a deep copy of the object, connect the signal to the one receiving object, emit the signal and disconnect again. This way we handle ownership transfer and ensure that only one object is copied/accessed.
// create deep copy of object as local object
QJsonObject clonedObject = QJsonObject::fromVariantMap(o.toVariantMap());
// create a queued connection to socket's thread
connect( this, SIGNAL(sendJsonMessage(QJsonObject)),
socket, SLOT(sendJsonObject(QJsonObject)),
Qt::QueuedConnection);
// transfer the data
emit sendJsonMessage(clonedObject);
// disconnect the signal
disconnect( this, SIGNAL(sendJsonMessage(QJsonObject)),
socket, SLOT(sendJsonObject(QJsonObject)));
// at end of function, local object goes out of scope and ownership is transfered to receiving thread
// create deep copy of object as local object
QJsonObject clonedObject = QJsonObject::fromVariantMap(o.toVariantMap());
// create a queued connection to socket's thread
connect( this, SIGNAL(sendJsonMessage(QJsonObject)),
socket, SLOT(sendJsonObject(QJsonObject)),
Qt::QueuedConnection);
// transfer the data
emit sendJsonMessage(clonedObject);
// disconnect the signal
disconnect( this, SIGNAL(sendJsonMessage(QJsonObject)),
socket, SLOT(sendJsonObject(QJsonObject)));
// at end of function, local object goes out of scope and ownership is transfered to receiving thread
To copy to clipboard, switch view to plain text mode
-Andreas
Bookmarks