Serialize existing non-generated QObject using QtProtobuf
semlanik edited this page 5 years ago

It's possible to serialize non-generated QObject using QtProbuf serializer. Few preconditions should be applied to the QObject-inherited class:

#include <qprotobufobject.h> //Include QtProtobuf macro definitions

class MyCustomObject : public QObject
{
    Q_OBJECT
    Q_PROTOBUF_OBJECT //Manually add QtProtobuf object definition
    Q_DECLARE_PROTOBUF_SERIALIZERS(MyCustomObject) //Manually define QtProtobuf serializers
    Q_PROPERTY(QString protoProperty READ protoProperty WRITE setProtoProperty NOTIFY protoPropertyChanged)
    ...
};

const std::unordered_map<int, int> MyCustomObject::propertyOrdering = {{1/*field number according .proto file*/,
                                                                        1/*property number, starts from 1*/},
                                                                        ...};