Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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

  • Moderators

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



  • @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/



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



  • @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)
    ...
    };
    

  • Moderators

    @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" 😉



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



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



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



  • @KroMignon Thanks, I got it.


Log in to reply