Important: Please read the Qt Code of Conduct -

Serializing custom global enum type in custom class

  • Hi, I have a global enum type defined outside of a class that contains a private property of that type.. now I would like to serialize that class in a qbytearray.. how can this be done?

  • Lifetime Qt Champion


    Do you mean using something like QDataStream ?

  • Hi, yes that is exactly what I mean! I have a class that I need to serialize with a QDataStream and it contains enum types (which are global not local to the class).. I know it can be done if the class inherits QOject and the enum is local to the class by QMetaObject but it's kinda tricky in that case.. If you know how to achieve that please share.. cuz right now the only solution I have is making the properties as qint which works fine but not quite what I wanted to do

  • One of solutions:
    @QDataStream &operator <<(QDataStream &out, const MyClass &packet)
    out <<quint8(packet.enumValue);
    return out;

    QDataStream &operator >>(QDataStream &in, MyClass &packet)
    packet = MyClass();
    quint8 tempVal;
    in >> tempNpt;
    packet.enumValue = enumType(tempVal);
    return in;

Log in to reply