Accessing an enum stored in a QVariant

Posted by Henry Thacker on Stack Overflow See other posts from Stack Overflow or by Henry Thacker
Published on 2010-04-01T12:24:09Z Indexed on 2010/04/01 13:13 UTC
Read the original article Hit count: 572

Filed under:

Hi,

I have registered an enumeration type "ClefType" within my header file - this enum is registered with the MetaObject system using the Q_DECLARE_METATYPE and Q_ENUMS macros. qRegisterMetaType is also called in the class constructor.

This allows me to use this type in a Q_PROPERTY, this all works fine. However, later on, I need to be able to get hold of the Q_PROPERTY of this enum type, given the object - in a form that is suitable for serialization.

Ideally, it would be useful to store the integer value for that enum member, because I don't want this to be specific to the type of enum that is used - eventually I want to have several different enums.

// 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 - custom enums don't seem to be directly 'castable' to an integer value.

Thanks in advance,

Henry

© Stack Overflow or respective owner

Related posts about qt4