Qt World Summit: Register Today!

Philosophical question on QML / C++ split

  • Hello,

    I was wondering what peoples experience or advice is with regards to the amount of code that should be placed in the C++ layer vs the QML/Javascript layer. For example, you could write virtually all of your application in QML/Javascript and not use much of C++ at all. Another approach is to use as much C++ as possible and only use QML for showing elements on the UI. For the given split, how will it scale for a non-trivial application? I'm basically trying to understand what would be a "best practise" approach to development using C++ and QML.


  • Moderators


    For "non-trivial" applications, you'd want a solution that minimizes the amount of code you have to write, and is easy to maintain. A good rule of thumb is to use each language for the purpose it was designed for.

    QML is designed for writing GUIs, so use it to write your GUI. It lets you describe complex GUIs with very little code (in contrast, C++ GUI code is a lot more verbose). JavaScript was included to let you write GUI logic.

    C++ is a general-purpose language that's good for computation, so use it to write your business logic. Compared to JavaScript, it lets you express your data better through a variety of data structures with encapsulation, which makes your program easier to maintain, which makes it easier to grow your program. C++ provides compile-time error checking too, which is also very useful for maintenance. Don't forget about performance either -- compiled C++ code will always outperform interpreted QML/JavaScript.

    However, guidelines have exceptions. There will be times when it's more convenient to do some GUI logic in C++, or some business logic in JavaScript. Only experience will teach how how to properly balance this.

  • So for example, let's say that I have a view-model type architecture where the UI layer is also required to perform some complex UI related processing and even keep UI related state, are you saying that I should still develop this in QML / Javascript? Or should this UI layer be split between two UI implementation langauges of C++ and QML / JavaScript? For example, I have seen that some QML files can become quite large and there is no encapsulation (except maybe double underscore for private variables, etc), how does this affect the design choices that need to be made?

  • For any reasonably complex application I tend to write as much as possible in C++ and expose the "business model" via QObjects. Keeping only essential UI stuff in QML and hook it to the C++ model (eg. state changes to model property changes, etc).

    If you need complex UI behavior and need to store UI releated data for your C++ model somewhere, and don't want to pollute your business model with UI stuff, maybe write a proxy model (also in C++) and put it there.

    I tend to prefer C++ over QML mostly because I am much more comfortable with C++. But things said in previous posts also apply: C++ is better in structuring your code. My experience is, that QML files tend to grow big and messy, even if you split into components. My main.qml is always the messiest one, with all the global states and transitions.

    To get further ideas, I recommend to watch "this video from Qt Dev Days 2012":http://www.youtube.com/watch?v=kvWeE3kurEQ It is the best example I've found so far, of how QML scales in realistically comlex applications.

  • Thanks for your answers they are very helpful

    @JKSH - "QML is designed for writing GUIs, so use it to write your GUI. It lets you describe complex GUIs with very little code" - Does this mean that if I find myself writing a lot of QML / Javascript code that it is an anti-pattern, especially as the QML / Javascript system does not seem to support a lot of the facilities you would except when having to write large systems, e.g. encapsulation...

    @martin_ky - I have found the same thing, it's easy to write specify complex behaviour with QML however I have found that the QML files do not scale very well and there seems to be no constructs to allow me to create a manageable system with a large amount of QML code; hence using C++ for a lot of it. Thanks for the video link.

    Part of the confusion for me came in when I discovered the mixed declarative / imperative programming model of the QML / Javascript system and hence knowing when to use QML / Javascript and when to use C++. I guess having to maintan these systems will bring some answers ;-)

Log in to reply