Q_ENUMS as property variant index



  • Hello all!
    I have the following problem: i declare enum in C++ and share it to QML context
    @
    class TestClass{
    Q_OBJECT
    Q_ENUMS(testEnum)
    public:
    enum testEnum{
    Low = 0,
    Mid = 1,
    High = 2
    };
    }
    @

    @
    qmlRegisterUncreatableType<TestClass>("CppImport", 1, 0, "TestClass", "NOT MAY CREATE");
    @

    In QML I'm trying to create property variant:
    @
    import QtQuick 1.1
    import CppImport 1.0

    QtObject{
    property variant dict:{
    TestClass.Low: "Low string",
    TestClass.Mid: "Mid string",
    TestClass.High: "High string",
    }
    }
    @

    I have this error:

    @Expected token `:' TestClass.Low: "Low string", @

    It is a bug? or I do not quite understand QML? Q_ENUMS should be represented as integers in QML. how to get around this error?

    My Qt version: 4.8.4. Sory for bad english.



  • It looks like you can only use literals to define keys.

    Eg:

    @
    property varaint map: {
    'string_literal': TestClass.Low,
    1024: 'some_value'
    }
    @

    You can access the first value like this:
    @map.string_literal@

    or like this:

    @map['string_literal']@

    and you can access the second value like this:

    @map[1024]@



  • Thanks for reply!
    I know, that indexes is a strings or digits. But enums it is a digits too!
    If i write
    @console.log(TestClass.Low)@
    i watch "1"

    If i write
    @console.log(1)@
    i watch "1"

    Why i do not write

    @ property variant dict:{
    TestClass.Low: "Low string",
    TestClass.Mid: "Mid string",
    TestClass.High: "High string",
    }@
    but i may write
    @
    property variant dict:{
    0: "Low string",
    1: "Mid string",
    2: "High string",
    }
    @
    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.