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. [Solved]Create enum for qml
Forum Updated to NodeBB v4.3 + New Features

[Solved]Create enum for qml

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.8k 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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    With the help of Q_PROPERTY, we could create property for qml
    what about enumerator?Is it possible to export the enumerator of c++
    to qml side?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There is no built-in way to have enums in QML. And it's not likely to be introduced soon - there are some technical hurdles on the way.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        Thanks, I will try another solution

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chrisadams
          wrote on last edited by
          #4

          You can use Q_ENUMS to export enum names to the QML typesystem. Internally, all enums are just treated as ints so if something doesn't work, change your property type to int and things should start working ;-)

          @
          class MyType : public QObject
          {
          Q_OBJECT
          Q_PROPERTY(MyEnum enumProp READ enumPropAccessor CONSTANT)
          Q_ENUMS(MyEnum)

          public:
          enum MyEnum {
          FirstValue = 1,
          SecondValue = 2
          };

          MyType(QObject *parent = 0) : QObject(parent) {}
          ~MyType() {}
          MyEnum enumPropAccessor() const { return FirstValue; }
          

          };
          @

          In QML, you should be able to do:

          @
          MyType {
          property int someValue: MyType.SecondValue
          }
          @

          Cheers,
          Chris.

          1 Reply Last reply
          0

          • Login

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