0

我有两个名为 IPCBase 和 DispatchData 的类。现在我想将 QDataStrean Object drom IPCBase 传递给 DispatchData。首先,我尝试使用 Connect Statement 直接发送它。但它给出了错误,例如 QDataStream 对象未在 QRegisterMatatype 中注册。

编辑::我也参考了这个链接

为 Qt 注册自定义类型时,何时、何地以及为何使用命名空间

所以我做了类似的事情

typedef QDataStream* myDataStrem;
Q_DECLARE_METATYPE(myDataStrem)

然后在另一个类中连接语句(DispatchData)

connect(mpThrIPCReceiver, SIGNAL(dispatchReadData(const int&, myDataStrem)),
        this, SLOT(onIPCDataReceived(const int&, myDataStrem)));

onIPCDataReceived Slot

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.
}
4

1 回答 1

0

似乎您正在传递一个悬空指针:当接收线程到达数据流时,它似乎不再存在。即使您在源对象中延长了它的生命周期,通过信号槽连接传递原始指针也是一个坏主意。如果源类可能在接收者线程有一个挂起的槽调用时消失,你仍然会在接收者处使用一个悬空指针。最好通过传递 aQSharedPointer或来为您服务std::shared_ptr

以下作品,您当然可以在共享指针中使用任何类型。

#include <QtCore>
#include <cstdio>

struct Class : public QObject {
   Q_SIGNAL void source(QSharedPointer<QTextStream>);
   Q_SLOT void destination(QSharedPointer<QTextStream> stream) {
      *stream << "Hello" << endl;
   }
   Q_OBJECT
};
Q_DECLARE_METATYPE(QSharedPointer<QTextStream>)

int main(int argc, char ** argv) {
   QCoreApplication app{argc, argv};
   Class c;
   c.connect(&c, &Class::source, &c, &Class::destination, Qt::QueuedConnection);
   auto out = QSharedPointer<QTextStream>(new QTextStream(stdout));
   emit c.source(out);
   QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
   *out << "About to exec" << endl;
   return app.exec();
}
#include "main.moc"

输出:

About to exec
Hello

在现代 Qt(至少 5.6)上,您不需要qRegisterMetatype在这种情况下调用。

同样使用std::shared_ptr

// https://github.com/KubaO/stackoverflown/tree/master/questions/datastream-pass-37850584
#include <QtCore>
#include <cstdio>
#include <memory>

struct Class : public QObject {
   Q_SIGNAL void source(std::shared_ptr<QTextStream>);
   Q_SLOT void destination(std::shared_ptr<QTextStream> stream) {
      *stream << "Hello" << endl;
   }
   Q_OBJECT
};
Q_DECLARE_METATYPE(std::shared_ptr<QTextStream>)

int main(int argc, char ** argv) {
   QCoreApplication app{argc, argv};
   Class c;
   c.connect(&c, &Class::source, &c, &Class::destination, Qt::QueuedConnection);
   auto out = std::make_shared<QTextStream>(stdout);
   emit c.source(out);
   QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
   *out << "About to exec" << endl;
   return app.exec();
}
#include "main.moc"
于 2016-06-16T19:16:11.537 回答