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] Registering an ENUM in QML
Forum Updated to NodeBB v4.3 + New Features

[solved] Registering an ENUM in QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 4.3k 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.
  • J Offline
    J Offline
    jduran
    wrote on last edited by
    #1

    Dear all,

    In the Qt documentation it is explained that enumerated types defined in a QObject derived class could be used in QML. However there is no sample of how to do the registration call per se. Could you provide a sample ?

    Also, it is possible to register uncreatable types. They are referring to enum types or a QObject derived class? Which are the requirements of theses classes? Could you provide a sample?

    Joaquim Duran

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hi, to register an enum you can simply use the "Q_ENUMS":http://qt-project.org/doc/qt-5/qobject.html#Q_ENUMS macro, e.g.
      @
      class Test : public QObject
      {
      Q_OBJECT
      Q_ENUMS(TestEnum)
      Q_PROPERTY(TestEnum testEnum READ getTestEnum WRITE setTestEnum NOTIFY testEnumChanged)

      public:
      explicit Test(QObject *parent = 0);

      enum class TestEnum {
          One, Two, Three
      };
      
      // getter, setter and signal omitted
      

      private:
      TestEnum m_testEnum;
      };
      @
      if you register this Test class to the QML engine you can just access the enum property and use it like Test.One etc.
      There might be one drawback, as far as I know enums are still converted to integer values, so you can't print the string value in QML, just the int :(
      I mean if you have a QML object of Test and you do this:
      @
      console.log(test.testEnum)
      @
      you will only see 0, 1 or 2 and not One, Two or Three sadly

      for you second question you can simply use "qmlRegisterUncreatableType":http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterUncreatableType or maybe better "qmlRegisterSingletonType":http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterSingletonType if you need a single object.

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jduran
        wrote on last edited by
        #3

        Xander84, your sample about enumerations it has been very useful.

        To access to the enumerated value is done in the same way as accessing to a property.

        Test {
        testEnum: Test.One
        }

        Joaquim Duran

        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