Get all enum value names from C++ enum definition
-
Hi,
System spec: Windows 10, Qt 6.9, mingw64 13.10, Qt Creator 17.0.1
Classic C++ enum side declaration.#include <QObject> #include <QtQml/qqml.h> class BackgroundType : public QObject { Q_OBJECT QML_ELEMENT QML_UNCREATABLE("BackgroundType is not creatable - use enum values only") public: explicit BackgroundType(QObject *parent = nullptr) : QObject(parent) {} enum Value { Basic, Upper, VIP }; Q_ENUM(Value) };
This class is NOT compiled with qt_add_qml_module. So I used
qmlRegisterUncreatableType<BackgroundType>("InfModule", 1, 0, "BackgroundType", "BackgroundTypeis not creatable - use enum values only");
Now, In QML side I would like to get list of all value names. For test purpose to scroll through them in combobox and check if all is fine.
Q_PROPERTY with static QStringList, Q_INVOKABLE returning QStringList - none of them work.
How to achive enum availabilty of uncreatable type with ability to print all enum values names? -
Partial solution which I found is to register this enum C++ class as
creatable
.
Then in point where I need value names - is to call is JS sectionvar obj = Qt.createQmlObject('import InfModule1.0; BackgroundType {}', parent); obj.names();
-
To be clear - I would like to call from QML side
BackgroundType.getValueNames();
and receiveQStringList("Basic", "Upper", "Vip")
.
Or my own QStrings mapping - likeQStringList("Basic Background", "Upper Level Background", "Vip Ultimate Background")
.PS:
Q_INVOKABLE QStringList fun() { return {"Basic"), "Upper", "VIP"}; }
it doesn't matter if it's registered with
qmlRegisterType
orqmlRegisterUncreatableType
or if I removeQML_UNCREATABLE
from dla in results - QML engine warns with
TypeError: Property 'names' of object InfModule/BackgroundType is not a function
-
Partial solution which I found is to register this enum C++ class as
creatable
.
Then in point where I need value names - is to call is JS sectionvar obj = Qt.createQmlObject('import InfModule1.0; BackgroundType {}', parent); obj.names();
-
S SebastianM has marked this topic as solved