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. Hot to register a native C/C++ enum to QML

Hot to register a native C/C++ enum to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 3 Posters 9.6k 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.
  • D Offline
    D Offline
    Dylan Deng
    wrote on 31 Aug 2020, 14:10 last edited by Dylan Deng
    #1

    I have some third-party libraries. They have defined some enumeration values. I hope to register these enumeration values for QML use. I don’t want to repeat them in the subclasses of Object. Is there any good way to make the front-end access? To the enumeration values of these third party libraries?

    enum {
        kIdle,
        kConnecting,
        kDisconnected
    }
    
    J K 2 Replies Last reply 31 Aug 2020, 16:41
    0
    • D Dylan Deng
      31 Aug 2020, 14:10

      I have some third-party libraries. They have defined some enumeration values. I hope to register these enumeration values for QML use. I don’t want to repeat them in the subclasses of Object. Is there any good way to make the front-end access? To the enumeration values of these third party libraries?

      enum {
          kIdle,
          kConnecting,
          kDisconnected
      }
      
      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 31 Aug 2020, 16:41 last edited by
      #2

      @Dylan-Deng I don‘t think so, IIRC than enums have to start with a capital letter, to be accessable in QML


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • D Dylan Deng
        31 Aug 2020, 14:10

        I have some third-party libraries. They have defined some enumeration values. I hope to register these enumeration values for QML use. I don’t want to repeat them in the subclasses of Object. Is there any good way to make the front-end access? To the enumeration values of these third party libraries?

        enum {
            kIdle,
            kConnecting,
            kDisconnected
        }
        
        K Offline
        K Offline
        KroMignon
        wrote on 31 Aug 2020, 21:34 last edited by KroMignon
        #3

        @Dylan-Deng Have you tried to find solution yourself?
        A simple query in any search machine, and you should find this
        ==> https://qml.guide/enums-in-qt-qml/

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        D 1 Reply Last reply 1 Sept 2020, 02:15
        0
        • K KroMignon
          31 Aug 2020, 21:34

          @Dylan-Deng Have you tried to find solution yourself?
          A simple query in any search machine, and you should find this
          ==> https://qml.guide/enums-in-qt-qml/

          D Offline
          D Offline
          Dylan Deng
          wrote on 1 Sept 2020, 02:15 last edited by Dylan Deng 9 Jan 2020, 03:50
          #4

          @KroMignon Yes, I have searched through the relevant information, which does not meet the existing needs. In many scenarios, we will access 3rd party SDKs to implement some functions, and the enumeration types that have been defined in these SDKs cannot be registered to QML in a simple way. They must be redeclared in the QObject subclass in order and provided to the front end Use, which goes against the original intention of enumeration, I think this is more like a new feature requirement.

          K 1 Reply Last reply 1 Sept 2020, 05:45
          0
          • D Dylan Deng
            1 Sept 2020, 02:15

            @KroMignon Yes, I have searched through the relevant information, which does not meet the existing needs. In many scenarios, we will access 3rd party SDKs to implement some functions, and the enumeration types that have been defined in these SDKs cannot be registered to QML in a simple way. They must be redeclared in the QObject subclass in order and provided to the front end Use, which goes against the original intention of enumeration, I think this is more like a new feature requirement.

            K Offline
            K Offline
            KroMignon
            wrote on 1 Sept 2020, 05:45 last edited by KroMignon 9 Jan 2020, 05:46
            #5

            @Dylan-Deng If you want to use a 3rd party SDK with QML, by the way you have to create QObject based interface.
            So why not add this enumeration to the QObject interface?

            class MySDK : public QObject
            {
                Q_OBJECT
                
            public:
                enum States {
                    Idle = kIdle,
                    Connecting = kConnecting,
                    Disconnected = kDisconnected
                };
                Q_ENUM(States)
            ...
            };
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            J D 2 Replies Last reply 1 Sept 2020, 06:03
            0
            • K KroMignon
              1 Sept 2020, 05:45

              @Dylan-Deng If you want to use a 3rd party SDK with QML, by the way you have to create QObject based interface.
              So why not add this enumeration to the QObject interface?

              class MySDK : public QObject
              {
                  Q_OBJECT
                  
              public:
                  enum States {
                      Idle = kIdle,
                      Connecting = kConnecting,
                      Disconnected = kDisconnected
                  };
                  Q_ENUM(States)
              ...
              };
              
              J Online
              J Online
              J.Hilk
              Moderators
              wrote on 1 Sept 2020, 06:03 last edited by J.Hilk 9 Jan 2020, 06:03
              #6

              @KroMignon said in Hot to register a native C/C++ enum to QML:

              you have to create QObject based interface.

              nope, no need for a full blown QObject class, Q_GADGET is enough

              class MySDK 
              {
                  Q_GADGET
                  
              public:
                  enum States {
                      Idle = kIdle,
                      Connecting = kConnecting,
                      Disconnected = kDisconnected
                  };
                  Q_ENUM(States)
              ...
              };
              

              much more "light weight" 😉


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              K 1 Reply Last reply 1 Sept 2020, 06:14
              0
              • J J.Hilk
                1 Sept 2020, 06:03

                @KroMignon said in Hot to register a native C/C++ enum to QML:

                you have to create QObject based interface.

                nope, no need for a full blown QObject class, Q_GADGET is enough

                class MySDK 
                {
                    Q_GADGET
                    
                public:
                    enum States {
                        Idle = kIdle,
                        Connecting = kConnecting,
                        Disconnected = kDisconnected
                    };
                    Q_ENUM(States)
                ...
                };
                

                much more "light weight" 😉

                K Offline
                K Offline
                KroMignon
                wrote on 1 Sept 2020, 06:14 last edited by KroMignon 9 Jan 2020, 06:15
                #7

                @J-Hilk said in Hot to register a native C/C++ enum to QML:

                nope, no need for a full blown QObject class, Q_GADGET is enough

                I think he want to access to some routines from his SDK, and then have to use slots and maybe signals for this, this is why I use QObject class.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • K KroMignon
                  1 Sept 2020, 05:45

                  @Dylan-Deng If you want to use a 3rd party SDK with QML, by the way you have to create QObject based interface.
                  So why not add this enumeration to the QObject interface?

                  class MySDK : public QObject
                  {
                      Q_OBJECT
                      
                  public:
                      enum States {
                          Idle = kIdle,
                          Connecting = kConnecting,
                          Disconnected = kDisconnected
                      };
                      Q_ENUM(States)
                  ...
                  };
                  
                  D Offline
                  D Offline
                  Dylan Deng
                  wrote on 7 Sept 2020, 01:40 last edited by
                  #8

                  @KroMignon A very important issue is that we need to redeclare these enumeration values in the QObject subclass. When the third party SDK adds or adjusts the order of enumeration, we need to re-adjust these definitions. This initially violates the original intention of enumeration, and this design is not particularly perfect.

                  K 1 Reply Last reply 7 Sept 2020, 05:33
                  1
                  • D Dylan Deng
                    7 Sept 2020, 01:40

                    @KroMignon A very important issue is that we need to redeclare these enumeration values in the QObject subclass. When the third party SDK adds or adjusts the order of enumeration, we need to re-adjust these definitions. This initially violates the original intention of enumeration, and this design is not particularly perfect.

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 7 Sept 2020, 05:33 last edited by KroMignon 9 Jul 2020, 05:48
                    #9

                    @Dylan-Deng said in Hot to register a native C/C++ enum to QML:

                    A very important issue is that we need to redeclare these enumeration values in the QObject subclass. When the third party SDK adds or adjusts the order of enumeration, we need to re-adjust these definitions. This initially violates the original intention of enumeration, and this design is not particularly perfect.

                    Indeed, but this is the only way to achieve it, if you want to have enum values directly accessible on QML side.
                    You want to connect different world, your SDK and QML, so you have to build the bridge between them.
                    I don't know anything about the SDK you want to interface with QML, but I did it with other SDK, and you can believe me, this is the easiest and quickest way to achieve it.

                    You can catch and handle all SDK specific routines in your interface class(es) and use it on QML side like a "native" QML object.
                    And it is clear that on every change on the SDK, you will have to adapt your interface classes. But this is true, event if you don't want to interface it with QML.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    D 1 Reply Last reply 7 Sept 2020, 05:46
                    0
                    • K KroMignon
                      7 Sept 2020, 05:33

                      @Dylan-Deng said in Hot to register a native C/C++ enum to QML:

                      A very important issue is that we need to redeclare these enumeration values in the QObject subclass. When the third party SDK adds or adjusts the order of enumeration, we need to re-adjust these definitions. This initially violates the original intention of enumeration, and this design is not particularly perfect.

                      Indeed, but this is the only way to achieve it, if you want to have enum values directly accessible on QML side.
                      You want to connect different world, your SDK and QML, so you have to build the bridge between them.
                      I don't know anything about the SDK you want to interface with QML, but I did it with other SDK, and you can believe me, this is the easiest and quickest way to achieve it.

                      You can catch and handle all SDK specific routines in your interface class(es) and use it on QML side like a "native" QML object.
                      And it is clear that on every change on the SDK, you will have to adapt your interface classes. But this is true, event if you don't want to interface it with QML.

                      D Offline
                      D Offline
                      Dylan Deng
                      wrote on 7 Sept 2020, 05:46 last edited by
                      #10

                      @KroMignon Thanks, I got it.

                      1 Reply Last reply
                      0

                      2/10

                      31 Aug 2020, 16:41

                      8 unread
                      • Login

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