Important: Please read the Qt Code of Conduct -

Exposing C++ classes to qml via context is good or bad?

  • Hi all, I'm working on an application and I want to use a controller-based architecture according to which I have a lot of classes (derived from QObject) and they are being exposed to qml so that it can bind to their properites, subscribe to signals and call functions. Now to expose controllers I'm using QQmlContext and setContextProperty, but I' m not sure whether this is a good approach.
    Could someone suggest an alternative approach? I would appreciate any comments :)

    Thanks in advance!

  • @diredko Technically speaking you're not exposing classes but objects. Another approach is to expose classes by registering them and then create objects in QML. Personally I don't see anything wrong with your approach, except that there are lots of global objects to be accessed in QML. As long as it's easy to maintain I don't see a reason to change it. But more experienced programmers may have other opinions.

Log in to reply