Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Serialize Custom Objects in Qt6

How to Serialize Custom Objects in Qt6

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 252 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neeraj Sharma
    wrote on last edited by Neeraj Sharma
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      N 1 Reply Last reply
      1
      • dheerendraD dheerendra

        @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>();

        N Offline
        N Offline
        Neeraj Sharma
        wrote on last edited by
        #3

        @dheerendra
        I did, it isn't working. Again some random character is printed.

        1 Reply Last reply
        1
        • N Offline
          N Offline
          Neeraj Sharma
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved