Access to the enumeration stored in QVariant

I registered the ClefType enumeration type in my header file - this enumeration was registered in the MetaObject system using the macros Q_DECLARE_METATYPE and Q_ENUMS. qRegisterMetaType is also called in the class constructor.

This allows me to use this type in Q_PROPERTY, all this works great. However, later I should be able to get Q_PROPERTY of this type of enumeration, given that the object is in a form suitable for serialization.

Ideally, it would be useful to keep an integer value for this enumeration element, because I do not want it to be specific to the type of enumeration that is used - after all, I want to have several different enumerations .

// This is inside a loop over all the properties on a given object QMetaProperty property = metaObject->property(propertyId); QString propertyName = propertyMeta.name(); QVariant variantValue = propertyMeta.read(serializeObject); // If, internally, this QVariant is of type 'ClefType', // how do I pull out the integer value for this enum? 

Unfortunately, variantValue.toInt(); does not work - user enumerations do not directly appear to be "castable" for an integer value.

Thanks in advance,

Henry

+4
source share
3 answers

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) 
0
source

Try:

 int x = variantValue.value<ClefType>(); 
+1
source

I had the same problem and I found a solution below that works for any of the above types:

 int x = property.enumerator().value(*reinterpret_cast<const int *>(variantValue.constData())); 
+1
source

Source: https://habr.com/ru/post/1305756/


All Articles