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. QML and C++11 Enums
Qt 6.11 is out! See what's new in the release blog

QML and C++11 Enums

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 5.3k Views 2 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.
  • freddy311082F Offline
    freddy311082F Offline
    freddy311082
    wrote on last edited by A Former User
    #1

    Hi guys... I have a litter question..

    Can I use C++11 enums in QML ??

    I can define my enums on my c++ class and export it to qml, but can I use instead normal enum a c++11 enum using:

    enum class A{v1,v2}

    ????

    that is my quesiton

    regards

    p3c0P 1 Reply Last reply
    0
    • freddy311082F freddy311082

      Hi guys... I have a litter question..

      Can I use C++11 enums in QML ??

      I can define my enums on my c++ class and export it to qml, but can I use instead normal enum a c++11 enum using:

      enum class A{v1,v2}

      ????

      that is my quesiton

      regards

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @freddy311082 Yes indeed we can definitely use. You have to declare it as a metatype using Q_DECLARE_METATYPE. I regularly use them. Check it here and usage is here.

      157

      freddy311082F S 2 Replies Last reply
      0
      • p3c0P p3c0

        Hi @freddy311082 Yes indeed we can definitely use. You have to declare it as a metatype using Q_DECLARE_METATYPE. I regularly use them. Check it here and usage is here.

        freddy311082F Offline
        freddy311082F Offline
        freddy311082
        wrote on last edited by
        #3

        @p3c0 said:

        here

        thanks... that answer my question

        rgards

        p3c0P 1 Reply Last reply
        0
        • freddy311082F freddy311082

          @p3c0 said:

          here

          thanks... that answer my question

          rgards

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @freddy311082 You're Welcome. Please mark the post as solved if done.

          157

          1 Reply Last reply
          0
          • p3c0P p3c0

            Hi @freddy311082 Yes indeed we can definitely use. You have to declare it as a metatype using Q_DECLARE_METATYPE. I regularly use them. Check it here and usage is here.

            S Offline
            S Offline
            seyed
            wrote on last edited by
            #5

            @p3c0 but how can you use following enums?

            enum class Animal { dog, deer, cat, bird, human };  // enum class
            enum class Mammal { kangaroo, deer, human };        // another enum class
            

            Note that deer is in both enum classes. How you distinguish these?

            raven-worxR freddy311082F 2 Replies Last reply
            0
            • S seyed

              @p3c0 but how can you use following enums?

              enum class Animal { dog, deer, cat, bird, human };  // enum class
              enum class Mammal { kangaroo, deer, human };        // another enum class
              

              Note that deer is in both enum classes. How you distinguish these?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @seyed
              you don't. You should name them already differently to make it clear. See Qt enums for example.

              enum Animal { dogAnimal, deerAnimal, catAnimal, birdAnimal, humanAnimal };
              enum Mammal { kangarooMammal , deerMammal , humanMammal  }
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • S seyed

                @p3c0 but how can you use following enums?

                enum class Animal { dog, deer, cat, bird, human };  // enum class
                enum class Mammal { kangaroo, deer, human };        // another enum class
                

                Note that deer is in both enum classes. How you distinguish these?

                freddy311082F Offline
                freddy311082F Offline
                freddy311082
                wrote on last edited by freddy311082
                #7

                @seyed

                Hi. I had the same issue and as Qt Enums doesn't let you use as you describe before, what I did was create a single class per Enum, also I export each class to QML to use it. So, if you have

                enum Animal { dogAnimal, deerAnimal, catAnimal, birdAnimal, humanAnimal };
                

                at least I did:

                class Animal : public QObject
                {
                    Q_OBJECT
                public:
                    Animal(QObject* parent = nullptr);
                    enum class AnimalEnum { dog, deer, cat, bird, human };;
                    virtual ~Animal(){}
                };
                
                Q_DECLARE_METATYPE(Animal::AnimalEnum);
                

                The same for Mammal enum. After that, you can register this classes to be used in QML, and you will be available to do

                Animal.deer
                Mammal.deer
                

                at least, is what I do when I have a situation like yours

                Regards

                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