Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Define Enum in coed header and Use it in qml

    QML and Qt Quick
    2
    7
    2129
    Loading More Posts
    • 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.
    • Jehyeok
      Jehyeok last edited by p3c0

      I would like to define a enum in cpp for globally to use in my qml.
      I referenced the documentation and follow the usage to make my own.
      However, it doesn't work in my case. Probably I used it wrong way.
      Could you please help me to have right code?

      This is my code
      type.h

      namespace AName
      {
      namespace BName
      {
      enum YourType
      {
      NONE = 0, TypeA = 1, TypeB = 2
      };
      }
      }
      

      A.h

      		typedef enum AName::BName::YourType MyType
      		Class A
      		{
      		Q_OBJECT
      		Q_ENUM(MyType)
      		...
      		}
      

      A.cpp

      		A::A()
      			rootContext()->setContextProperty("qmlA", this);
      

      xxx.qml
      (I am able to access qmlA::xxxxx())

      Text {
      property var type: 0
      Component.onCompleted: {
      **	if (type == qmlA.TypeA)** (!!!!!! This doesn't work it returns undefined !!!!!!)
      		return
      }
      }
      
      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Jehyeok last edited by

        @Jehyeok AFAIK setting context property won't work. You will need to register that class using qmlRegisterType. import it in QML and you are ready to use it.

        157

        Jehyeok 1 Reply Last reply Reply Quote 1
        • Jehyeok
          Jehyeok @p3c0 last edited by

          @p3c0 Could you give me some details ?

          I tried this but it doesn't work.

          namespace AName
          {
          namespace BName
          {
          enum YourType
          {
          NONE = 0, TypeA = 1, TypeB = 2
          };
          }
          }

          A.h

              Class A
              {
              Q_OBJECT
          

          public:
          typedef enum AName::BName::YourType MyType
          Q_ENUM(MyType)
          ...
          }

          main.cpp
          qmlRegisterTypenamespaces::A("Namespaces.A", 1, 0, "A");

          .qml
          import Namespaces.A 1.0
          Text {
          property var type: 0
          Component.onCompleted: {
          ** if (type == A.TypeA)** (!!!!!! This doesn't work it returns undefined !!!!!!)
          return
          }
          }

          p3c0 1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators @Jehyeok last edited by

            @Jehyeok Since Q_Enum is declared in ClassA, register ClassA.

            157

            Jehyeok 1 Reply Last reply Reply Quote 1
            • Jehyeok
              Jehyeok @p3c0 last edited by

              @p3c0 I am able to use a enum that I declared in the resisted class. But, typedef enum didn't work.
              Do you know how to do it? Otherwise I have to have duplicated enum in two files...

              p3c0 1 Reply Last reply Reply Quote 0
              • p3c0
                p3c0 Moderators @Jehyeok last edited by

                @Jehyeok Are those enums in the class with Q_OBJECT markers ? You can try creating a similar class and then include it in the registered class.

                157

                Jehyeok 1 Reply Last reply Reply Quote 0
                • Jehyeok
                  Jehyeok @p3c0 last edited by

                  @p3c0 BC I would like to keep the header enum types regardless Q_Object. I just redefined enums with Q_Object for qml. Thank you for help :)

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post