using loader or dynamically create component

  • I would like to seek your opinion in this case:

    I have an app with different layouts in every platform, so I created different Forms.

    Which is more viable to use, loader or create component dynamically depending on platform. Does loader work efficiently and not use too much memory?

    I am having problem passing properties when creating object using createObject function. In my Form component i used repeater to display components into column. I used VisualItemModel for form components, however i encountered few errors while doing it.

    Item {
      property alias model: repeater.model
      Column {
        anchors.fill: parent
        spacing: 0
        Repeater {
          id: repeater
     // Page
     Item {
       // This is how I set the form in static
       Form {
         model: VisualItemModel {
           Checkbox {}
           Switch {}
           TextField {}

    Can you please advise how to create the form using createObject function?

    I tried creating static VisualItemModel in each platform and pass it through in createObject however, the unused model encountered an error due to null parent.

    Please advise. Thanks.

  • Qt Champions 2016

    @literA2 you should take a llok at a session from Qt Summit 2015 about Memory Management,
    where this is explained in detail:

    also about Effective QML:
    (goto Tip #15)

    and about QML Mistakes:
    (goto #4 about Loader vs createObject()

    also there's a documentation:

    I would go with a Loader and set it to async mode to avoid blocking

  • Qt offers a solution called "file selectors" for loading platform specific files in a manner that is entirely transparent to the app. QQmlApplicationEngine sets it up for you automatically, so all you need is to organize the platform-specific variants into +<platform> subfolders.

  • Qt Champions 2016

    @jpnurmi cool. learned about file selectors from your reply. sounds good.

  • Thank you @ekkescorner for your inputs, I'll take time to view and read them.

    @jpnurmi thank you! I'm actually aware of the fileselector however, we're not inclined to use it because we wanted to limit the qml pages for different layouts. However, using it into components is possible.

  • Notice that there's a major difference between file selectors and dynamic component creation. The former is in many cases superior from performance perspective. You won't spend extra CPU cycles for dynamically loading something that never changes, and more importantly, it encourages you to write simpler QML code.

  • @jpnurmi thanks for additonal infos.

    I noticed this when using QQmlFileSelector, i needed to create a blank Form qml file on parent directory of these folders: +android, +ios. And the imports on platform specific forms should follow as if it resided on its parent directory. Or i should decide which form to use as default. am i correct?

  • Yes, with file selectors you always need a default/fallback file. Otherwise an alternate won't be looked up. Which of the two alternates serves a better default if/when your app is run outside the two platforms? :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.