Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Q_ENUMS as property variant index
Forum Updated to NodeBB v4.3 + New Features

Q_ENUMS as property variant index

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oosavu
    wrote on 9 Apr 2013, 04:23 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      melghawi
      wrote on 12 Apr 2013, 11:15 last edited by
      #2

      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]@

      1 Reply Last reply
      0
      • O Offline
        O Offline
        oosavu
        wrote on 15 Apr 2013, 04:22 last edited by
        #3

        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",
        }
        @
        ?

        1 Reply Last reply
        0

        3/3

        15 Apr 2013, 04:22

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved