Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

QENUM is used to register an enum type so that it can be used in QML. To use QENUM, you need to declare the enum in your C++ class using the Q_ENUM macro. For example:

class MyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(MyEnum enumValue READ enumValue NOTIFY valueChanged)
    Q_ENUM(MyEnum)
public:
    enum MyEnum {
        Option1,
        Option2,
        Option3
    };
    MyEnum enumValue() const { return m_enumValue; }
signals:
    void valueChanged();
private:
    MyEnum m_enumValue;
};

In this example, we have declared an enum class called MyEnum and registered it using the QENUM macro. We have also declared a property in our class called "enumValue" of type MyEnum and registered it using the QPROPERTY macro.

To use this enum in QML, we can simply import our C++ class and access the enum as follows:

import MyNamespace 1.0
...
Dropdown {
    model: Object.Option
}

In this code, we are using the MyNamespace class that we have imported and accessing the enum values using the dot notation (Object.Option). Dropdown is an example of QML element that uses an enum as a model.

Note that if you change the values of your enum class, you need to recompile your code and rerun qmake for the changes to take effect.