Results 1 to 2 of 2

Thread: Exposing a Q_ENUM to QML

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Exposing a Q_ENUM to QML

    I have an QObject derived class, which I want to use in QML. It has no UI component, just some properties, one of which is of type 'MyEnum'. MyEnum is declared with the Q_ENUMS() macro..

    Qt Code:
    1. class MyClass : public QObject {
    2. Q_OBJECT
    3. Q_ENUMS(myEnum)
    4.  
    5. public:
    6. MyClass() : t(FirstValue) { }
    7. enum myEnum { InvalidValue, FirstValue, SecondValue } ;
    8.  
    9. testType testVal() const { return t; }
    10. Q_PROPERTY(myEnum testVal READ testVal NOTIFY testValChanged)
    11. private:
    12. testType t;
    13.  
    14. signals:
    15. void testValChanged();
    16. };
    To copy to clipboard, switch view to plain text mode 

    Now I inject this QObject into my main.qml using setContextProperty(). All the regular properties work fine, and when ask for the property of type 'MyEnum'.. I get the integer value, which ok, not so bad.. but there's no way to get the Key names. Trying to access 'MyObject.InvalidValue , MyObject.FirstValue etc return undefined.

    It differs from doc.qt.nokia.com/4.7-snapshot/qtbinding.html#using-enumerations-of-a-custom-type
    In the example, the object is created from QML rather than C++, and the enumeration (defined in C++) is used freely..

    Is this expected behavior? A work-around would be an extra property returning the string name. Any other ways?
    Last edited by aidanok; 16th December 2010 at 17:27.

  2. #2
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exposing a Q_ENUM to QML

    Ah, missed something obvious, you need to access the Enum with through the class name, rather than the object name:

    MyClass.FirstValue returns '1'

    MyObject.FirstValue returns 'undefined'

Similar Threads

  1. Exposing custom widget properties
    By Plixil in forum Newbie
    Replies: 1
    Last Post: 25th July 2010, 17:38
  2. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 17:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.