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

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 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.
  • J Offline
    J Offline
    Jehyeok
    wrote on 16 May 2016, 12:13 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
    }
    }
    
    P 1 Reply Last reply 16 May 2016, 12:40
    0
    • J Jehyeok
      16 May 2016, 12:13

      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
      }
      }
      
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 16 May 2016, 12:40 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

      J 1 Reply Last reply 16 May 2016, 17:20
      1
      • P p3c0
        16 May 2016, 12:40

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

        J Offline
        J Offline
        Jehyeok
        wrote on 16 May 2016, 17:20 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
        }
        }

        P 1 Reply Last reply 16 May 2016, 17:58
        0
        • J Jehyeok
          16 May 2016, 17:20

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

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 16 May 2016, 17:58 last edited by
          #4

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

          157

          J 1 Reply Last reply 17 May 2016, 01:42
          1
          • P p3c0
            16 May 2016, 17:58

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

            J Offline
            J Offline
            Jehyeok
            wrote on 17 May 2016, 01:42 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...

            P 1 Reply Last reply 17 May 2016, 07:23
            0
            • J Jehyeok
              17 May 2016, 01:42

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

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 17 May 2016, 07:23 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

              J 1 Reply Last reply 18 May 2016, 01:13
              0
              • P p3c0
                17 May 2016, 07:23

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

                J Offline
                J Offline
                Jehyeok
                wrote on 18 May 2016, 01:13 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

                1/7

                16 May 2016, 12:13

                • Login

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