[Interest] How to use enum as QML method return type?

xizhi.zhu at nokia.com xizhi.zhu at nokia.com
Thu Dec 1 08:40:29 CET 2011


Hi,

Suppose I have the following code:
class MyObject : public QObject
{
    Q_OBJECT
    Q_ENUMS(MyEnum)
    Q_PROPERTY(MyEnum myEnum READ myEnum)

public:
    enum MyEnum {
        Value1 = 0,
        Value2
    };

    explicit MyObject(QObject *parent = 0);

    MyEnum myEnum() const;

    Q_INVOKABLE MyEnum getMyEnum() const;
};

Then I register this class:
qmlRegisterType<MyObject>("myobject", 1, 0, "MyObject");

But in QML:
console.log(myObject.myEnum)  // prints 0, as expected
console.log(myObject.myEnum == MyObject.Value1)  // prints true, as expected
console.log(myObject.getMyEnum())  // prints undefined
console.log(myObject.getMyEnum() == MyObject.Value1)  // prints false


So did I missing something? Or should I use "int" as the return type?
Any hints?


================
Xizhi Zhu (Steven)

Software Engineer @ Qt Development Frameworks
Nokia

Mobile: +358 (0)50 480 1247


More information about the Interest mailing list