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

C++ enum to QML ComboBox

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml
9 Posts 3 Posters 1.7k 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.
  • B Offline
    B Offline
    Babs
    wrote on 10 Jul 2019, 12:25 last edited by
    #1

    Hello is there any way to use directly Enum from C++ as QML ComboBox model?

    G 1 Reply Last reply 10 Jul 2019, 12:36
    0
    • B Babs
      10 Jul 2019, 12:25

      Hello is there any way to use directly Enum from C++ as QML ComboBox model?

      G Offline
      G Offline
      Gojir4
      wrote on 10 Jul 2019, 12:36 last edited by
      #2

      @Babs Hi,

      If your enums are defined in a QObject subclass, you can extract list of them quite easily using Qt Meta-Object System

      J B 2 Replies Last reply 10 Jul 2019, 12:38
      2
      • G Gojir4
        10 Jul 2019, 12:36

        @Babs Hi,

        If your enums are defined in a QObject subclass, you can extract list of them quite easily using Qt Meta-Object System

        J Online
        J Online
        JonB
        wrote on 10 Jul 2019, 12:38 last edited by JonB 7 Oct 2019, 12:39
        #3

        @Gojir4
        Should the OP also possibly read through https://woboq.com/blog/q_enum.html? Does that (Q_ENUM) give a convenient wrapper?

        G 1 Reply Last reply 10 Jul 2019, 12:46
        3
        • J JonB
          10 Jul 2019, 12:38

          @Gojir4
          Should the OP also possibly read through https://woboq.com/blog/q_enum.html? Does that (Q_ENUM) give a convenient wrapper?

          G Offline
          G Offline
          Gojir4
          wrote on 10 Jul 2019, 12:46 last edited by
          #4

          @JonB You're right. I should have precised that Q_ENUM macro is required to register the enum in the Meta-Object system.
          What do you mean by "a convenient wrapper" ?

          J 1 Reply Last reply 10 Jul 2019, 12:49
          2
          • G Gojir4
            10 Jul 2019, 12:46

            @JonB You're right. I should have precised that Q_ENUM macro is required to register the enum in the Meta-Object system.
            What do you mean by "a convenient wrapper" ?

            J Online
            J Online
            JonB
            wrote on 10 Jul 2019, 12:49 last edited by
            #5

            @Gojir4
            I don't do Qt C++. I didn't know whether you can make meta-object calls directly without the need for that macro. Or of course you could write the code directly, for all I know you might have said whatever the Q_ENUM macro is isn't a "convenient wrapper to access the meta-system" here, you don't like it or you don't want to have to write C++ to access from QML, or something like those.

            1 Reply Last reply
            0
            • G Gojir4
              10 Jul 2019, 12:36

              @Babs Hi,

              If your enums are defined in a QObject subclass, you can extract list of them quite easily using Qt Meta-Object System

              B Offline
              B Offline
              Babs
              wrote on 10 Jul 2019, 12:50 last edited by
              #6

              @Gojir4 I define my enum class like this

              class QTOBJECTDICTONNARY_EXPORT  CanOpenNmtCmdSpecifs {
                  Q_GADGET
              
              public:
                  enum Enum {
                      StartNode = 0x01,
                      StopNode  = 0x02,
                      SetPreOp  = 0x80,
                      ResetNode = 0x81,
                      ResetComm = 0x82,
                  };
                  Q_ENUM (Enum)
              };
              typedef CanOpenNmtCmdSpecifs::Enum CanOpenNmtCmdSpecif;
              

              And try to expose it to QML in that way

              qmlRegisterType<CanOpenNmtCmdSpecif>("NMTCmd",1,0,"NMTCmd");
              

              The problem is that i get the error : staticMetaObject' is not a member of 'CanOpenNmtCmdSpecifs::Enum'
              const char *className = T::staticMetaObject.className();
              ~~~~~~~~~~~~~~~~~~~~^
              Is the Q_GADGET use instead of Q_OBJECT the probblem?

              G 1 Reply Last reply 10 Jul 2019, 13:00
              0
              • B Babs
                10 Jul 2019, 12:50

                @Gojir4 I define my enum class like this

                class QTOBJECTDICTONNARY_EXPORT  CanOpenNmtCmdSpecifs {
                    Q_GADGET
                
                public:
                    enum Enum {
                        StartNode = 0x01,
                        StopNode  = 0x02,
                        SetPreOp  = 0x80,
                        ResetNode = 0x81,
                        ResetComm = 0x82,
                    };
                    Q_ENUM (Enum)
                };
                typedef CanOpenNmtCmdSpecifs::Enum CanOpenNmtCmdSpecif;
                

                And try to expose it to QML in that way

                qmlRegisterType<CanOpenNmtCmdSpecif>("NMTCmd",1,0,"NMTCmd");
                

                The problem is that i get the error : staticMetaObject' is not a member of 'CanOpenNmtCmdSpecifs::Enum'
                const char *className = T::staticMetaObject.className();
                ~~~~~~~~~~~~~~~~~~~~^
                Is the Q_GADGET use instead of Q_OBJECT the probblem?

                G Offline
                G Offline
                Gojir4
                wrote on 10 Jul 2019, 13:00 last edited by
                #7

                @Babs You're declaring the enum and not the type here:

                qmlRegisterType<CanOpenNmtCmdSpecif>("NMTCmd",1,0,"NMTCmd");
                

                Should not it be :

                qmlRegisterType<CanOpenNmtCmdSpecifs>("NMTCmd",1,0,"NMTCmd");
                

                Also I'm not sure it works with the typedef, and it's unecessary in c++ you can write.

                 enum CanOpenNmtCmdSpecif{
                        StartNode = 0x01,
                        StopNode  = 0x02,
                        SetPreOp  = 0x80,
                        ResetNode = 0x81,
                        ResetComm = 0x82,
                    };
                    Q_ENUM (CanOpenNmtCmdSpecif)
                
                B 1 Reply Last reply 10 Jul 2019, 14:13
                0
                • G Gojir4
                  10 Jul 2019, 13:00

                  @Babs You're declaring the enum and not the type here:

                  qmlRegisterType<CanOpenNmtCmdSpecif>("NMTCmd",1,0,"NMTCmd");
                  

                  Should not it be :

                  qmlRegisterType<CanOpenNmtCmdSpecifs>("NMTCmd",1,0,"NMTCmd");
                  

                  Also I'm not sure it works with the typedef, and it's unecessary in c++ you can write.

                   enum CanOpenNmtCmdSpecif{
                          StartNode = 0x01,
                          StopNode  = 0x02,
                          SetPreOp  = 0x80,
                          ResetNode = 0x81,
                          ResetComm = 0x82,
                      };
                      Q_ENUM (CanOpenNmtCmdSpecif)
                  
                  B Offline
                  B Offline
                  Babs
                  wrote on 10 Jul 2019, 14:13 last edited by
                  #8

                  @Gojir4 Indeed the register doesn't work with typeDef. Thank you for your assistance.
                  Regards,

                  G 1 Reply Last reply 10 Jul 2019, 14:25
                  1
                  • B Babs
                    10 Jul 2019, 14:13

                    @Gojir4 Indeed the register doesn't work with typeDef. Thank you for your assistance.
                    Regards,

                    G Offline
                    G Offline
                    Gojir4
                    wrote on 10 Jul 2019, 14:25 last edited by
                    #9

                    @Babs You're welcome ! Please don't forget to set the post as solved if your issue is fixed. Thanks !

                    1 Reply Last reply
                    1

                    9/9

                    10 Jul 2019, 14:25

                    • Login

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