Ask Your Question
3

How can Q_ENUM and Q_PROPERTY be utilized for an enum class in QML?

asked 2023-05-05 18:44:32 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-05 18:53:01 +0000

djk gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-05 18:44:32 +0000

Seen: 13 times

Last updated: May 05 '23