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

QML_INTERFACE example



  • Hello

    I've already asked a question about QML_INTERFACE on stackoverflow but I feel like this is a pretty specialized topic so I thought I should probably ask again here.

    I've a hard time understanding how QML_INTERFACE and QML_IMPLEMENTS_INTERFACES are supposed to be used. A very basic code example of how I initially thought those macros work can be found over at stackoverflow but spoiler alert, it couldn't get it to work at all.

    What confused me further was a quote from the book "Cross-Platform Development with Qt 6 and Modern C++" which states that:
    QML_INTERFACE registers an existing Qt interface type. The type is not instantiable from QML, and you cannot declare QML properties with it.

    I can't figure out if that's true or false? In case it is true doesn't it kinda defeat the whole purpose of having an interface in the first place? What's the point of having an interface if I can't have it use "virtual properties"?

    I've later figured out that it's perfectly fine to combine the QML_ELEMENT and QML_UNCREATABLE macros to create interfaces which can't be instantiated by the QML engine but solely from C++. From C++ one can then pass base class pointers to concrete implementations to the QML engine. This is more or less what I wanted to achieve, so I'm fine with that.

    That I don't understand QML_INTERFACE still bothers me though. :)


  • Qt Champions 2018

    The point of QML_INTERFACE is that a type can implements multiple of them.
    A QML_INTERFACE should not inherit from QObject, the type which will have QML_IMPLEMENTS_INTERFACE will though.

    I believe it only makes sense to have an interace with virtual functions, not properties.


Log in to reply