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. Define Enum in coed header and Use it in qml
QtWS25 Last Chance

Define Enum in coed header and Use it in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.5k Views
  • 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.
  • JehyeokJ Offline
    JehyeokJ Offline
    Jehyeok
    wrote on last edited by p3c0
    #1

    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
    }
    }
    
    p3c0P 1 Reply Last reply
    0
    • JehyeokJ Jehyeok

      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
      }
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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

      JehyeokJ 1 Reply Last reply
      1
      • p3c0P p3c0

        @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.

        JehyeokJ Offline
        JehyeokJ Offline
        Jehyeok
        wrote on last edited by
        #3

        @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
        }
        }

        p3c0P 1 Reply Last reply
        0
        • JehyeokJ Jehyeok

          @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
          }
          }

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

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

          157

          JehyeokJ 1 Reply Last reply
          1
          • p3c0P p3c0

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

            JehyeokJ Offline
            JehyeokJ Offline
            Jehyeok
            wrote on last edited by
            #5

            @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...

            p3c0P 1 Reply Last reply
            0
            • JehyeokJ Jehyeok

              @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...

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

              @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

              JehyeokJ 1 Reply Last reply
              0
              • p3c0P p3c0

                @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.

                JehyeokJ Offline
                JehyeokJ Offline
                Jehyeok
                wrote on last edited by
                #7

                @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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved