PDA

View Full Version : QMetaEnum, QMetaProperty for dynamically added property



pkj
3rd June 2013, 14:10
I need to come up with a property browser (for dynamic properties) like interface table for QObject derived class objects. Properties can be enumerated using QObject::dynamicPropertyNames() function. For each entry, I intend to read the property from the object using QObject::property(). That will take care of the reading part.
For modifying data, I need to depend on the type of variant returned by QObject::property() and set the delegate appropriately for the type. Here comes the problem with enumeration types. For enum, I want to have a delegate which shows a combo box with all the enumeration value strings. For Q_Property like properties(lets call it static properties), I can access the metaObject(), determine whether enum, get the QMetaEnum using QMetaProp::enumerator() and get it done easily.
However, dynamic properties the QVariant 1st problem is type is int for the enums. I can overcome that by Q_Declare_MetaType(SomeClass::EnumName) and qRegisterMetaType<SomeClass::EnumName>("nameMyEnum").
Now the QVariant I get from QObject::property() will give the type as "nameMyEnum" rather than int.
But how do i get the QMetaEnum so that I can get all the related enum values and populate the combo box.

anda_skoa
3rd June 2013, 17:03
Make SomeClass a QObject, create some mapping of enum name to meta object, e.g.


QHash<QByteArray, QMetaObject*> EnumMap;
EnumMap.insert("nameMyEnum", &SomeClass::staticMetaObject);


Cheers,
_

pkj
3rd June 2013, 17:45
Thanks for the suggestion.
SomeClass is already a QObject derived class. Had to make it one to use Q_ENUMS, Q_DECLARE_METATYPE, qRegisterMetaType. However, there are a few of these SomeClass like classes. Currently runs into hundreds, and expected to grow further. Hardcoding for each one of these class into a hash will be too prone to break. I will have to introduce some kind of registration scheme for each class.
Even that will end up being messy, given the fact that the enums types are int in qvariant. To get them to show some type I need to qRegisterMetaType for each of these. Given most of the enums of qt gui types are not registered, the code will be too prone to error as the author of a class may not introduce the qRegisterMetaType for each type.
I wish there was a easier way out, like it is with static properties iteration and type classification. Too much to ask for, perhaps.