Serialize nested user defined class in Q_PROPERTY
-
I faced with following problem: I can't serialize user defined object from Q_PROPERTY
I try to serialize RegistersSettings class to QDataStream. The idea is to be able to serialize it to text file (using << operator) and later be able to read it (using >> operator). It should verify that fields that was readed from file are still valid. So I inspect property for that.
The issue is that Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) is not work as expected.
It looks like it's possible to create such functionality, but it looks that it's not so easy.
Could anyone help with the common way how to serialize user defined class from Q_PROPERTY?The code is simplifyed to be more readable, but the main idea is in place.
class RegisterGroupSettings:SettingsItem<RegisterGroupSettings> { private: Q_GADGET Q_PROPERTY(QString name MEMBER name) Q_PROPERTY(int interval MEMBER interval) public: QString name; int interval; }; Q_DECLARE_METATYPE(RegisterGroupSettings) class RegistersSettings:SettingsItem<RegistersSettings> { private: Q_GADGET Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) Q_PROPERTY(int code MEMBER code) public: QList<RegisterGroupSettings> groups; int code; }; Q_DECLARE_METATYPE(RegistersSettings)
SettingsItem is a helper fo unification
template <typename T> class SettingsItem { public: friend QDataStream & operator << (QDataStream &arch, const T & object) { const QMetaObject &mo = object.staticMetaObject; int cnt = mo.propertyCount(); QString prop_name; QVariant prop_value; arch << cnt; while (cnt>0) { prop_name = mo.property(cnt-1).name(); prop_value = mo.property(cnt-1).readOnGadget(&object); arch << prop_name; arch << prop_value; cnt--; } return arch; } friend QDataStream & operator >> (QDataStream &arch, T & object) { const QMetaObject &mo = object.staticMetaObject; int cnt=0; QString prop_name; QVariant prop_value; int prop_index; arch >> cnt; while (cnt>0) { arch >> prop_name; arch >> prop_value; prop_index = mo.indexOfProperty(prop_name.toStdString().c_str()); if (prop_index > -1) { mo.property(prop_index).writeOnGadget(&object, prop_value); } cnt--; } return arch; } friend bool operator == (const T &first, const T &second) { const QMetaObject &mo = first.staticMetaObject; int cnt = mo.propertyCount(); QString prop_name; QVariant oProp_value; QVariant dProp_value; while (cnt>0) { prop_name = mo.property(cnt-1).name(); oProp_value = mo.property(cnt-1).readOnGadget(&first); dProp_value = mo.property(cnt-1).readOnGadget(&second); if (oProp_value == dProp_value) { cnt--; continue; } return false; } return true; } friend bool operator != (const T &first, const T &second) { return !( first == second ); } };
-
I faced with following problem: I can't serialize user defined object from Q_PROPERTY
I try to serialize RegistersSettings class to QDataStream. The idea is to be able to serialize it to text file (using << operator) and later be able to read it (using >> operator). It should verify that fields that was readed from file are still valid. So I inspect property for that.
The issue is that Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) is not work as expected.
It looks like it's possible to create such functionality, but it looks that it's not so easy.
Could anyone help with the common way how to serialize user defined class from Q_PROPERTY?The code is simplifyed to be more readable, but the main idea is in place.
class RegisterGroupSettings:SettingsItem<RegisterGroupSettings> { private: Q_GADGET Q_PROPERTY(QString name MEMBER name) Q_PROPERTY(int interval MEMBER interval) public: QString name; int interval; }; Q_DECLARE_METATYPE(RegisterGroupSettings) class RegistersSettings:SettingsItem<RegistersSettings> { private: Q_GADGET Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) Q_PROPERTY(int code MEMBER code) public: QList<RegisterGroupSettings> groups; int code; }; Q_DECLARE_METATYPE(RegistersSettings)
SettingsItem is a helper fo unification
template <typename T> class SettingsItem { public: friend QDataStream & operator << (QDataStream &arch, const T & object) { const QMetaObject &mo = object.staticMetaObject; int cnt = mo.propertyCount(); QString prop_name; QVariant prop_value; arch << cnt; while (cnt>0) { prop_name = mo.property(cnt-1).name(); prop_value = mo.property(cnt-1).readOnGadget(&object); arch << prop_name; arch << prop_value; cnt--; } return arch; } friend QDataStream & operator >> (QDataStream &arch, T & object) { const QMetaObject &mo = object.staticMetaObject; int cnt=0; QString prop_name; QVariant prop_value; int prop_index; arch >> cnt; while (cnt>0) { arch >> prop_name; arch >> prop_value; prop_index = mo.indexOfProperty(prop_name.toStdString().c_str()); if (prop_index > -1) { mo.property(prop_index).writeOnGadget(&object, prop_value); } cnt--; } return arch; } friend bool operator == (const T &first, const T &second) { const QMetaObject &mo = first.staticMetaObject; int cnt = mo.propertyCount(); QString prop_name; QVariant oProp_value; QVariant dProp_value; while (cnt>0) { prop_name = mo.property(cnt-1).name(); oProp_value = mo.property(cnt-1).readOnGadget(&first); dProp_value = mo.property(cnt-1).readOnGadget(&second); if (oProp_value == dProp_value) { cnt--; continue; } return false; } return true; } friend bool operator != (const T &first, const T &second) { return !( first == second ); } };
@EagleNN said in Serialize nested user defined class in Q_PROPERTY:
The issue is that Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) is not work as expected.
You need t declare the list specialization as a metatype, provide stream operators for it, and finally register that stream operators with the meta-type system. Qt doesn't know about your specialization of that template class and without these things so it can't put it into a
QVariant
, neither can it write it to the data stream, nor can it be read from the stream.PS.
Also I advise you use proper static access for static members, i.e.:T::staticMetaObject
. -
@EagleNN said in Serialize nested user defined class in Q_PROPERTY:
The issue is that Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) is not work as expected.
You need t declare the list specialization as a metatype, provide stream operators for it, and finally register that stream operators with the meta-type system. Qt doesn't know about your specialization of that template class and without these things so it can't put it into a
QVariant
, neither can it write it to the data stream, nor can it be read from the stream.PS.
Also I advise you use proper static access for static members, i.e.:T::staticMetaObject
.@kshegunov said in Serialize nested user defined class in Q_PROPERTY:
You need t declare the list specialization as a metatype,
RegisterGroupSettings is declared as metatype (Q_DECLARE_METATYPE(RegisterGroupSettings))
provide stream operators for it,
Stream operators << and >> provided from template class SettingsItem
and finally register that stream operators with the meta-type system.
So class RegistersSettings should be extended with constructor?
RegistersSettings() { qRegisterMetaTypeStreamOperators<RegisterGroupSettings>("RegisterGroupSettings"); qRegisterMetaTypeStreamOperators<QList<RegisterGroupSettings>>("QList<RegisterGroupSettings>"); }
-
What error are you getting exactly ?
-
The solution is to extend template with constructor
SettingsItem() { qRegisterMetaType<T>(); qRegisterMetaTypeStreamOperators<T>(T::staticMetaObject.className()); }
and register nested type in class constructor
RegistersSettings() { qRegisterMetaTypeStreamOperators<QList<RegisterGroupSettings>>("QList<RegisterGroupSettings>"); }