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. How to define new enums in QML?
Forum Updated to NodeBB v4.3 + New Features

How to define new enums in QML?

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 10.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.
  • E Offline
    E Offline
    Etchelon
    wrote on last edited by
    #1

    Sorry if the answer lies somewhere in the documentation already, I usually try to find everything online but in this case I could not.

    How can I define new enums directly in QML? I know that you can mark any enum defined in a C++ class with the Q_ENUMS macro, but in my case I want to define an enum that describes various states of a QtQuick component I'm crafting.
    Alternatively, if I define a Q_ENUMS in one C++ class that I later import in some part of my QtQuick project, how can I make those enums visible outside of the class?
    For example:
    I define class MyClass { Q_ENUMS enum MyStates { off, on }; };
    Then I register this type in the namespace "MyNamespace" and write this in main.qml:
    @import MyNamespace 1.0
    Item {
    MyNamespace { id: ns }
    }@
    At this point, can I use those enums in another QML file? For example, in MyComponent.qml
    @Item {
    property var state: MyClass.off
    }@
    Will that work? I mean, take for example Text.NativeRendering. I guess that's an enum, and is globally accessible in the QML type system once registered, is that correct?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Etchelon
      wrote on last edited by
      #2

      @#ifndef ENUMS_HPP
      #define ENUMS_HPP

      #include <QObject>

      class Enums : public QObject
      {
      Q_OBJECT

      Q_ENUMS(AngleUnits)

      public:
      enum AngleUnits
      {
      degrees,
      radians,
      centesimal
      };

      // This is a wrapper, uncreatable type
      Enums() = delete;
      Enums(const Enums&) = delete;
      Enums& operator=(const Enums&) = delete;
      };

      #endif // ENUMS_HPP
      @

      @int main(int argc, char* argv[])
      {
      // stuff
      qmlRegisterUncreatableType<Enums>("Shared", 1, 0, "Enums", "Enums is not a type, just a wrapper for enums used in the program");
      // stuff
      return app.exec();
      }
      @

      @import Shared 1.0
      Item {
      Component.onCompleted: console.log(Enums.centesimal);
      }
      @

      Why does this print undefined?? What am I doing wrong?

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Etchelon
        wrote on last edited by
        #3

        Anyone?? ...

        1 Reply Last reply
        0
        • strahlexS Offline
          strahlexS Offline
          strahlex
          wrote on last edited by
          #4

          Try to inherit QQuickItem instead of QObject. It is the default for creating QML Objects with QtQuick 2.

          Btw. there are currently some problems with posting to this forum.

          Feel free to check out my website machinekoder.com
          and my pet projects Intellicute and QtQuickVcp

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Etchelon
            wrote on last edited by
            #5

            Thanks, I tried. Doesn't change anything, my enums are still printed as undefined...

            Does this silence by "Qt Certified Specialists" mean they don't ever need to define custom enums in their programs/apps? :)

            1 Reply Last reply
            0
            • strahlexS Offline
              strahlexS Offline
              strahlex
              wrote on last edited by
              #6

              Here is an example with working Q_ENUMS:

              @#ifndef PERSON_H
              #define PERSON_H

              #include <QQuickItem>
              #include <QDebug>

              class Person : public QQuickItem
              {
              Q_OBJECT
              Q_INTERFACES(QQmlParserStatus)
              Q_ENUMS(TestEnum)
              Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
              QString m_name;

              public:
              explicit Person(QQuickItem *parent = 0);

              enum TestEnum {
                  Test1,
                  Test2,
                  Test3
              };
              
              QString name() const
              {
                  return m_name;
              }
              
              virtual void componentComplete()
              {
                  // Perform some initialization here now that the object is fully created
                  qDebug() << m_name;
              }
              

              signals:

              void nameChanged(QString arg);

              public slots:

              void setName(QString arg)
              {
              if (m_name != arg) {
              m_name = arg;
              emit nameChanged(arg);
              }
              }
              };

              #endif // PERSON_H@

              Maybe the enum names must be Uppercase because they are constants?

              Feel free to check out my website machinekoder.com
              and my pet projects Intellicute and QtQuickVcp

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Etchelon
                wrote on last edited by
                #7

                Damn right you are! This was it! Wow...why is it not mentioned anywhere in the docs?
                Thx man, you made my day :)

                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