Qt Forum

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

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

    QML and Qt Quick
    3
    10
    2768
    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.
    • D
      Dylan Deng last edited by Dylan Deng

      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.Hilk KroMignon 2 Replies Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Dylan Deng last edited by

        @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

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

        1 Reply Last reply Reply Quote 0
        • KroMignon
          KroMignon @Dylan Deng last edited by KroMignon

          @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 Reply Quote 0
          • D
            Dylan Deng @KroMignon last edited by Dylan Deng

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

            KroMignon 1 Reply Last reply Reply Quote 0
            • KroMignon
              KroMignon @Dylan Deng last edited by KroMignon

              @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.Hilk D 2 Replies Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @KroMignon last edited by J.Hilk

                @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

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                KroMignon 1 Reply Last reply Reply Quote 0
                • KroMignon
                  KroMignon @J.Hilk last edited by KroMignon

                  @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 Reply Quote 0
                  • D
                    Dylan Deng @KroMignon last edited by

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

                    KroMignon 1 Reply Last reply Reply Quote 1
                    • KroMignon
                      KroMignon @Dylan Deng last edited by KroMignon

                      @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 Reply Quote 0
                      • D
                        Dylan Deng @KroMignon last edited by

                        @KroMignon Thanks, I got it.

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