Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Loader Component and bindings
Forum Update on Monday, May 27th 2025

Loader Component and bindings

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 2.7k Views
  • 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.
  • A Offline
    A Offline
    albertoMirada
    wrote on last edited by
    #1

    Hi all:

    I have an app with some optional modules. In these modules, i need to load some qml files only in case the module is loaded, so I will use the Loader component to load the files. I do that because the source qml file could could not be accesible in case the module is not loaded, so I will use something like: source: Manager.moduleLoaded ? "path/module.qml" : ""

    My problem is trying to access the root item of the qml. "item" property is read only, so I cannot set properties in that item, or set bindings. The problem of setting values to properties could be solved with setter functions, but I consider the problem of bindings the most important.

    Have any of you one idea of how to resolve this issue?? any help would be appreciated.

    Thanks in advance

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chriadam
      wrote on last edited by
      #2

      Hi,

      I haven't tried it myself, but can you have a proxy property?

      @
      Item {
      id: root
      Loader {
      id: loader
      source: Manager.moduleLoaded ? "path/module.qml" : "";
      }

      property QtObject proxy: loader.item
      
      // setting property should work from js expressions
      function whatever() {
          if (proxy) {
              proxy.someProperty = 42;
          }
      }
      
       // bindings should work
       property int boundProperty: proxy ? proxy.someProperty : 5;
      

      }
      @

      But as I said, I haven't tested it.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        Can you provide a small snippet of code demonstrating the issues you are having? Note that even though the item property is read-only, you should still be able to manipulate the properties of item (you cannot replace item itself, but you can change its properties). For example, the following should result in the loaded item turning green:

        @Loader {
        source: "MyRect.qml"
        onLoaded: item.color = "green"
        }@

        Regards,
        Michael

        1 Reply Last reply
        0
        • A Offline
          A Offline
          albertoMirada
          wrote on last edited by
          #4

          Thanks again. I dont have any code yet because im still thinking about the design. I will try to write an example:

          @
          main.qml

          Rectangle {
          id: mainContainer
          property int value: 5
          property bool available: false

          Loader {
          id: myLoader
          source: Manager.moduleEnabled ? "path/ModuleLoaded.qml" : ""
          //this is the problematic line
          item.players: moduleStatic.players

          }
          property QtObject proxy: myLoader.item

          MyModule {
          id: moduleStatic
          }
          }
          @

          @
          MyModule.qml

          Rectangle{
          id: moduleStatic
          property int players: 3
          ...

          onStateChanged: {
          players = 5
          }
          }
          @

          @
          ModuleLoaded.qml

          Rectangle{
          id: moduleDynamic
          property int players: 3
          ...

          }
          @

          What I need is doing something like the commented line (line 12). with this binding, if players property of moduleStatic changes its value internally (for example, with the onStateChanged{}), the players property of the moduleDynamic (that was loaded by the Loader) will update its value due to the binding.

          Talking about read-only item property, is interesting to know that, although item is read-only, the item properties arent, so I can write proxy.property1 = 45 without any workaround. Thanks for that.

          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