Loader Component and bindings



  • 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



  • 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.



  • 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



  • 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.


Log in to reply
 

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