Git Product home page Git Product logo

Comments (3)

Skycoder42 avatar Skycoder42 commented on July 20, 2024

With the current implementation, this is not directly possible. The reason is that gadgets are value types and c++ can simply not handle polymorphism for value types correctly. Also, gadgets do not have a non-static metaobject associated, which means it's impossible to dynamically detect their type.

However, if you are fine with a workaround, I guess the following would work:

Assuming you have the following (simplified) classes:

class GadgetBase {
    //...
};

class Gadget1 : public GadgetBase {
    // ...
};

class Gadget2 : public GadgetBase {
    // ...
};

class SomeOtherGadget {
    GadgetBase *theGadget; //should be either 1 or 2
};

You could implement SomeOtherGadget as follows:

class SomeOtherGadget {
    Q_GADGET

    Q_PROPERTY(QJsonObject theGadget READ getGad WRITE setGad)

    GadgetBase *theGadget;
    QJsonSerializer *serializer;

    SomeOtherGadget() {
        serializer = // get the serializer somehow or just create a new one
    }

    QJsonObject getGad() const {
        QJsonObject obj;
        QString type = getGadgetClassName(theGadget);
        if(type == "Gadget1")
            obj = serializer->serialize(*static_cast<Gadget1*>(theGadget));
        else if(type == "Gadget2")
            obj = serializer->serialize(*static_cast<Gadget2*>(theGadget));
        // ...
        obj["@class"] = type;
        return obj;
    }

    void setGad(const QJsonObject &obj) {
        if(obj["@class"] == "Gadget1")
            theGadget = new Gadget1(serializer->deserialize<Gadget1>(obj)); //copy-construct to the heap
        else if(obj["@class"] == "Gadget2")
            theGadget = new Gadget2(serializer->deserialize<Gadget2>(obj)); //copy-construct to the heap
        // ...
    }
};

It's a little complicated and verbose, but more or less the only way to make gadget serialization "polymorphic" (I used a special property name @class here to keep the same syntax in JSON as the QObject based classes. But you can of course store that information however you like). I hope this can help you

from qtjsonserializer.

Skycoder42 avatar Skycoder42 commented on July 20, 2024

Another alternative would be to implement a custom QJsonTypeConverter that does more or less the same thing. If you have many classes that need this polymorphism, using a custom converter would be much more convenient I guess.

from qtjsonserializer.

arietto avatar arietto commented on July 20, 2024

Thank you for your huge reply. I suppose that you code sample will be helpful for other users too.

from qtjsonserializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.