Hi, to register an enum you can simply use the Q_ENUMS [qt-project.org] macro, e.g.
class Test : public QObject
{
Q_OBJECT
Q_ENUMS(TestEnum)
Q_PROPERTY(TestEnum testEnum READ getTestEnum WRITE setTestEnum NOTIFY testEnumChanged)
public:
explicit Test(QObject *parent = 0);
enum class TestEnum {
One, Two, Three
};
// getter, setter and signal omitted
private:
TestEnum m_testEnum;
};
if you register this Test class to the QML engine you can just access the enum property and use it like Test.One etc.
There might be one drawback, as far as I know enums are still converted to integer values, so you can’t print the string value in QML, just the int :(
I mean if you have a QML object of Test and you do this:
console.log(test.testEnum)
you will only see 0, 1 or 2 and not One, Two or Three sadly
for you second question you can simply use qmlRegisterUncreatableType [qt-project.org] or maybe better qmlRegisterSingletonType [qt-project.org] if you need a single object.
↧










