Declarative Thinking for Qt Developers/Expose C++ Data Structures With No-QObject Derived Classes to QML

Declarative Thinking for Qt Developers

“Do this, do that.” Throughout the history of computer programming, engineers have mostly spent their time writing their programs in a specific manner. This manner has dominated the programming world, and most programs that we love and use today are written using this imperative style.


In contrast to the imperative style, declarative style deals with relationships and the description of uses. Transitioning from a traditional style of building UIs with widgets and imperative calls to a more declarative way of doing things with QML is not a straightforward one.


In order to fully embrace QML and all its benefits, we need to make a mental model shift from an imperative design to a declarative design.

Expose C++ Data Structures With No-QObject Derived Classes to QML

If you have a large code base with custom data structures that need to be exposed to the UI, you’ve probably considered various ways of doing it by inheriting from QObject or using QQmlPropertyMap.


The approach we took has increased developer productivity significantly. We are exposing our C++ data structures directly to QML by creatively using Qt’s meta object system and creating a bi-directional binding between the internal data structure and the QML properties. With this method, we can even expose internal lambdas to QML and directly call them. And also benefit from the speed improvements that the QML compiler provides.