You should put the enum in your class that derives from QObject
. Also it should be marked with Q_ENUMS
macro. You can then take the QMetaEnum
of your enum from the metaobject of the class, iterate through the keys and their values and add each one to a QStringList
.
In C++ it would be like :
MyClass myObj;
const QMetaObject* metaObj = myObj.metaObject();
QMetaEnum enumType = metaObj->enumerator(metaObj->indexOfEnumerator("MyEnumType"));
QStringList list;
for(int i=0; i < enumType.keyCount(); ++i)
{
QString item = QString::fromLatin1(enumType.key(i)) + " "
+ QString::number(enumType.value(i));
list.append(item);
}
Now you can use QQmlContext::setContextProperty
to expose the the string list to QML :
QQmlContext *context = engine.rootContext();
context->setContextProperty("myModel", QVariant::fromValue(list));
You would have a combo-box containing the enum key and values when the ComboBox
item is like :
ComboBox {
model: myModel
...
}