Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Philosophical question on QML / C++ split
Qt 6.11 is out! See what's new in the release blog

Philosophical question on QML / C++ split

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    John

    1 Reply Last reply
    0
    • JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • martin_kyM Offline
          martin_kyM Offline
          martin_ky
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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 ;-)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved