You can use the >> and << QVariant operators to accomplish this.
Saving (where MyClass *x = new MyClass(this); and out - QDataStream ):
const QMetaObject *pObj = x->pObj(); for(int id = pObj->propertyOffset(); id < pObj->propertyCount(); ++id) { QMetaProperty pMeta = pObj->property(id); if(pMeta.isReadable() && pMeta.isWritable() && pMeta.isValid()) { QVariant variantValue = pMeta.read(x); out << variantValue; } }
Loading:
const QMetaObject *pObj = x->pObj(); for(int id = pObj->propertyOffset(); id < pObj->propertyCount(); ++id) { QMetaProperty pMeta = pObj->property(id); if(pMeta.isReadable() && pMeta.isWritable() && pMeta.isValid()) { QVariant variantValue; in >> variantValue; pMeta.write(x, variantValue); } }
You will need to call
qRegisterMetaType<CMyClass::ClefType>("ClefType"); qRegisterMetaTypeStreamOperators<int>("ClefType");
in addition to using Q_OBJECT , Q_ENUMS and Q_PROPERTY . Calling qRegisterMetaTypeStreamOperators<int> tells Qt to use the int versions of operator<< and operator>> .
By the way: using qRegisterMetaType<CMyClass::ClefType>() instead of the form that takes the name does not work for me. This may be if you used the returned identifier to search for the name, but it is much simpler.
FYI, here is the definition of MyClass :
class CMyClass : public QObject { Q_OBJECT Q_ENUMS(ClefType) Q_PROPERTY(ClefType cleftype READ getCleftype WRITE setCleftype) public: CMyClass(QObject *parent) : QObject(parent), m_cleftype(One) { qRegisterMetaType<CMyClass::ClefType>("ClefType"); qRegisterMetaTypeStreamOperators<int>("ClefType"); } enum ClefType { Zero, One, Two, Three }; void setCleftype(ClefType t) { m_cleftype = t; } ClefType getCleftype() const { return m_cleftype; } private: ClefType m_cleftype; }; Q_DECLARE_METATYPE(CMyClass::ClefType)
source share