Multithreading 通过QVector<;浮动>;通过信号/插槽从工作线程到主线程
目前,我在将QVector传递到线程之间时遇到一些问题。目前,我有一个主线程(GUI线程)和一个工作线程,它们经常发出QVector数组。在发出数据之前,向量内部看起来不错。接收器是主线程中的一个插槽,但插槽接收到的数据是乱码 以下是我的代码的一些部分: 在工作线程中发出:Multithreading 通过QVector<;浮动>;通过信号/插槽从工作线程到主线程,multithreading,qt,signals-slots,qthread,Multithreading,Qt,Signals Slots,Qthread,目前,我在将QVector传递到线程之间时遇到一些问题。目前,我有一个主线程(GUI线程)和一个工作线程,它们经常发出QVector数组。在发出数据之前,向量内部看起来不错。接收器是主线程中的一个插槽,但插槽接收到的数据是乱码 以下是我的代码的一些部分: 在工作线程中发出: void Pipeline::process { QVector<float> buffer(w * h * d); // filling the vector with RGB-Values
void Pipeline::process
{
QVector<float> buffer(w * h * d);
// filling the vector with RGB-Values
emit this->pushBuffer(buffer, w, h, d);
}
void管道::进程
{
矢量缓冲器(w*h*d);
//用RGB值填充向量
发出此->推送缓冲区(缓冲区,w,h,d);
}
信号与主螺纹槽的连接:
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::connect(this->_pipeline.data(),信号(pushBuffer(const QVector,int,int)),this->ui->widgetFiltered,SLOT(setBuffer(const QVector,int,int));
主线程中的插槽:
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
}
void GLWidget::setBuffer(常量QVector缓冲区、int-dataSizeX、int-dataSizeY、int-dataSizeZ)
{
//此时,“缓冲区”内的内容被篡改
}
通过使用QObject的moveToThread和通过qRegisterMetaType(“QVector”)注册到元系统的QVector
启动线程代码>在主方法中
Pipeline::process
返回后数据是否可能丢失?我不确定QVector
中的隐式共享在这种多线程情况下的行为
任何帮助都将不胜感激
问候
Wolfa)寄存器元类型QVector。在主函数中的app.exec()
之前添加此行:
qRegisterMetaType<QVector<float> >("QVector<float>");
qRegisterMetaType(“QVector”);
没有这个QueuedConnection将无法工作
b) 明确地说,您的信号和插槽通过Qt::QueuedConnection
如果您在连接后移动到线程,这将修复正确线程中的插槽执行。您好,谢谢您的回答。我的主函数中已经有了一个qRegisterMetaType(“QVector”)
,我只是忘了发布它。正如您所说,我已经添加了Qt::QueuedConnection
,但它仍然是乱七八糟的:-(.奇怪-如果使用排队连接,qt会复制数据,因此即使超出范围,您也应该拥有vector的副本…您确定vector在发出之前包含正确的数据,并且代码中没有其他内容不会破坏内存吗?嗯,我刚刚发现,如果不将vtk链接到我的项目中,此连接工作正常。非常奇怪…:-D。