【发布时间】:2016-06-16 05:33:04
【问题描述】:
我有两个名为 IPCBase 和 DispatchData 的类。现在我想将 QDataStrean Object drom IPCBase 传递给 DispatchData。首先,我尝试使用 Connect Statement 直接发送它。但它给出了错误,例如 QDataStream 对象未在 QRegisterMatatype 中注册。
edit :: 我也参考了这个链接
When, where and why use namespace when registering custom types for Qt
所以我做了类似的事情
typedef QDataStream* myDataStrem;
Q_DECLARE_METATYPE(myDataStrem)
然后在另一个类中连接语句(DispatchData)
connect(mpThrIPCReceiver, SIGNAL(dispatchReadData(const int&, myDataStrem)),
this, SLOT(onIPCDataReceived(const int&, myDataStrem)));
onIPCDataReceived 插槽
void DispatchData::onIPCDataReceived(const int& msgType, myDataStrem dataReceived)
{
// dataReceived >> str1; Here it is giving error
// qDebug()<<"is"<<str1;
MemberFuncPointer f = mIPCCommandMapper.value(msgType);
(this->*f)(*dataReceived);
//This is function pointer which will rout it to respective function depending on the Message type.
然后它会来这里
void DispatchData::onStartCountingCycle(QDataStream &dataReceived)
{
int data = 0;
dataReceived >> data; //Here it is crashing
//Giving error like
//pure virtual method called
//terminate called without an active exception
// I have debugged it and here dataReceived is becoming Readonly.
}
【问题讨论】:
-
我可以帮助你,但请提供一个有效的例子!
-
您似乎正在使用线程,这就是为什么它会自动使您的连接成为排队连接。在这种情况下,您将不得不为
QDataStream致电qRegisterMetatype,因为您收到的消息会告诉您。但这对我来说仍然很奇怪,你为什么要跨线程发送QDataStream对象?解释一下你实际上想做什么,这对我来说就像一个XY problem。 -
@thuga 这里不需要
qRegisterMetatype。 -
@KubaOber 没错,我刚刚收到了他收到的错误消息。
标签: c++ qt qdatastream