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 problem
Forum Updated to NodeBB v4.3 + New Features

Q_ENUMS problem

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 6 Posters 13.2k 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.
  • A Offline
    A Offline
    aabc
    wrote on last edited by
    #1

    How can i use an enum that was declared in another class in qml ?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      You don't have to call Q_ENUMS in the class the enum was defined in. If you use the full qualifier name, you can do it in basically any class. Hope that helps.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aabc
        wrote on last edited by
        #3

        The problem is that if i define an enum in one class and than i want to use in in another class:

        @
        ClassA
        {
        ...
        enum someEnum{E1, E2, E3}
        ...
        }

        ClassB
        {
        ...
        Q_ENUMS(ClassA::someEnum)
        ...
        }
        @

        and than to use in in qml it does not work

        [EDIT: code formatting, please use @-tags, Volker]

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          Which class exactly do you want to use in qml?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aabc
            wrote on last edited by
            #5

            I want to use class that i create myself (inherit QObject)

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              OK, after doing some research and testing, here's the situation:
              The only way you can make it work is using the Macros Q_GADGET (or of course inherit QObject etc.) and Q_ENUMS(someEnum) within ClassA (I didn't find any other way, but maybe somebody else on the forum has a solution).

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aabc
                wrote on last edited by
                #7

                I do use Q_ENUMS in class A but when i try to use that enum in a Q_INVOKABLE function of class B (in QMl) i can't.
                Do you know why?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jim_kaiser
                  wrote on last edited by
                  #8

                  Like @loladiro pointed out.. unless you intend to use the enum in signal/slot connections or something which needs Qt's metaobject system to recognize your enum, you just access it as A::someEnum in B.

                  From Qt Documentation : "If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS()."

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aabc
                    wrote on last edited by
                    #9

                    The problem is that i want to use it in QML - not in c++.

                    what does it mean "must be fully qualified with the name of the class defining it" ?

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DenisKormalev
                      wrote on last edited by
                      #10

                      To use enums in qml you need to

                      • use Q_ENUMS
                      • use Q_DECLARE_METATYPE(YourClass::YourEnum)
                      • use qmlRegisterUncreatableType<YourClass>("YourPackage", 1, 0, "YourClass", "")
                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aabc
                        wrote on last edited by
                        #11

                        I know how to use enum with Q_ENUMS.

                        My question is:
                        How can i use enum that was declared in another class in qml ?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          loladiro
                          wrote on last edited by
                          #12

                          Ok I took the time to make an example (excuse the sloppy naming). This works and is accessible in QML:
                          @class Test2
                          {
                          Q_GADGET
                          public:
                          Q_ENUMS(Test)
                          Test2() {}
                          enum Test { V1=100, V2, V3 };
                          };

                          class EnumTest : public QObject
                          {
                          Q_OBJECT
                          public:
                          Q_PROPERTY(Test2::Test test23 READ get3 WRITE set3 NOTIFY changed3 )
                          EnumTest() : test23(Test2::V3) {}
                          Test2::Test test23;
                          Test2::Test get3() {return test23;}
                          void set3(Test2::Test t) {test23=t;emit changed3();}
                          signals:
                          void changed3();
                          };
                          @
                          This doesn't (and I found no way to make it work without changing the Test2 class):
                          @
                          class Test2
                          {
                          public:
                          Test2() {}
                          enum Test { V1=100, V2, V3 };
                          };

                          class EnumTest : public QObject
                          {
                          Q_OBJECT
                          public:
                          Q_PROPERTY(Test2::Test test23 READ get3 WRITE set3 NOTIFY changed3 )
                          Q_ENUMS(Test2::Test)
                          EnumTest() : test23(Test2::V3) {}
                          Test2::Test test23;
                          Test2::Test get3() {return test23;}
                          void set3(Test2::Test t) {test23=t;emit changed3();}
                          signals:
                          void changed3();
                          };
                          @

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            Tân Ngọc Đỗ
                            wrote on last edited by
                            #13

                            Hello, You can register enum to meta data system :

                            @qRegisterMetaTypeYourClass::YourEnum("YourClass::YourEnum");
                            @

                            It runs ok

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              onezeno
                              wrote on last edited by
                              #14

                              [[blank-post-content-placeholder]]

                              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