Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing Qt's Q_ENUMS to QML

Tags:

enums

qt

qml

I might be missing something obvious here, but when trying to expose a Q_ENUM to QML, even in the most simple case, does not seem to work as shown in the QT docs (http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#using-enumerations-of-a-custom-type)

I've created a simple test case, my C++ class looks like:

class MyClass : public QDeclarativeItem {
    Q_OBJECT
    Q_ENUMS(testType)

public:
    MyClass() : t(FirstValue) {  }
    enum testType { InvalidValue, FirstValue, SecondValue } ;

    testType testVal() const { return t; }
    Q_PROPERTY(testType testVal READ testVal NOTIFY testValChanged)
private:
    testType t;

signals:
    void testValChanged();
};

I then register & inject an instance of this class into the QDeclartiveContext.

When I try and access the testVal property, it returns the integer (1 in this case) rather than a string representation. In addition, with the instance injected as 'aVar', if I try and access 'aVar.FirstValue', the result is 'undefined'

So this means I can't do tests like: 'if aVar.testVal == FirstValue' (ReferenceError for the unqualified FirstValue)

Or like this: 'if aVar.testVal == aVar.FirstValue' (aVar.FirstValue is undefined)

Anyone been through this before? It seems to conflict with example provided in the QT docs, although, the Object is instantiated from QML in that example, so this might be the cause..

like image 701
aidanok Avatar asked Dec 16 '10 13:12

aidanok


1 Answers

Enum values can only be accessed as "ElementName.EnumValue", not "object.EnumValue". So, aVar.FirstValue won't work; you'll need to use MyClass.FirstValue instead (and to do this, you'll need to register MyClass with qmlRegisterType() and then import the registered module).

Also, enum values as not returned as strings since they are defined as integer values.

like image 192
blam Avatar answered Oct 09 '22 15:10

blam