How to Serialize Custom Objects in Qt6
-
I have a custom class Dummy which I want to send as an object using Dynamic Replica in Qt6. I'm able to send the class but it's being transferred as QVariant which I'm not able to extract(or cast) from QVariabnt object. Below is my implementation:
Dummy.h file
#ifndef CLIENT_DUMMY_H #define CLIENT_DUMMY_H #include <QtCore/qobject.h> #include <QtCore/qdatastream.h> #include <QtCore/qvariant.h> #include <QtCore/qmap.h> #include <QtCore/qmetatype.h> #include <QtRemoteObjects/qremoteobjectnode.h> #include <QtRemoteObjects/qremoteobjectsource.h> #include <QtCore> class Dummy { Q_GADGET Q_PROPERTY(QString m_name READ name WRITE setName) public: Dummy(){} explicit Dummy(QString str) : m_name(str) {} QString name() const { return m_name; } void setName(QString str){ m_name = str; } ~Dummy() = default; private: QString m_name; }; Q_DECLARE_METATYPE(Dummy) inline QDataStream &operator<<(QDataStream &ds, const Dummy &obj) { QtRemoteObjects::copyStoredProperties(&obj, ds); return ds; } inline QDataStream &operator>>(QDataStream &ds, Dummy &obj) { QtRemoteObjects::copyStoredProperties(ds, &obj); return ds; } inline bool operator==(const Dummy &left, const Dummy &right) Q_DECL_NOTHROW { return left.name() == right.name(); } inline bool operator!=(const Dummy &left, const Dummy &right) Q_DECL_NOTHROW { return !(left == right); } inline QDebug operator<<(QDebug dbg, const Dummy &obj) { dbg.nospace() << "Dummy(" << "m_name: " << obj.name() << ")"; return dbg.maybeSpace(); }
The class is present at both sides server as well as client.
.rep file
class Interface { PROP(Dummy dummy); [...removed extra code] }
Server method sending data to client:
void send() { Dummy dummy("DummyString"); setDummy(dummy); }
Client side file:
Inside constructor:
QObject::connect(reptr.data(), SIGNAL(dummyChanged(Dummy)), this, SLOT(receiveDummy(Dummy))); void DynamicClient::receiveDummy(Dummy dummy) { if(reptr.data()->isReplicaValid()){ QVariant variant = reptr.data()->property("dummy"); qDebug() << variant; } }
But when object from server to client is sent, qDebug() prints below: QVariant(Dummy, QVariant(QString, "DummyString"))
I'm not able to extract Dummy Object from Qvariant object.
I've tried registering my custom type using qRegisterMetaType() as well but still it didn't work. Apart from that, I've used qvariant_cast and variant.value<Dummy>() but when I print the value I get some random character each time.
Thanks in advance. I can post more code if required.
-
@Neeraj-Sharma said in How to Serialize Custom Objects in Qt6:
Dummy, QVariant(QString, "DummyString")
Did you try something like this ?
Dummy dm = variant.value<Dummy>(); -
@Neeraj-Sharma said in How to Serialize Custom Objects in Qt6:
Dummy, QVariant(QString, "DummyString")
Did you try something like this ?
Dummy dm = variant.value<Dummy>();@dheerendra
I did, it isn't working. Again some random character is printed. -
Turns out I was not registering the Dummy class on client side due to which client was not able to recognize the type.
qRegisterMetaType needs to be used at both ends.