Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

A problem transfering a parameter through a loader.



  • Hi to all,

    i am experiencing a strange problem with the loader mechanism.

    The calling document instantiates the loader and pass to it a series of properties defined in the loader QML document. Here is a small example showing the mechanism:

    MyApp.qml includes

    @
    Item {
    id: appItem

    // Properties to customize the loader document
    property string theme: "xxx"
    property string title: "The object title"

    [...]

    // Loader component instance
    MyLoader {
    id: loaderInstance

    // Properties assignement
    theme: appItem.theme
    title: appaItem.title
    
    [... other stuff ...]
    
    // Absolute document name
    obj: "DocToLoad"
    

    }
    @

    Thus MyLoader.qml is defined as follows:

    @
    Item {
    id: docLoader

    property string theme: ""
    property string title: ""
    property string obj: ""

    Loader {
    id: loadObj

    [... various stuff ...]
    function getTheme() { return docLoader.theme; }
    function getTitle() { return docLoader.title; }
    
    source: docLoader.obj + ".qml"
    

    [... code managing component signals ...]
    }
    @

    The problem happens in the loaded object. Take in account that despite this example there is a lot of other parameters passed from the calling application to the destination component throught the loader and all the other works.
    The DocToLoad.qml is defined as follows:

    @
    Item {
    id: loadableObject

    [... the loadable object code ...]

    text {
    id: myText

    [ ...]
    
    text: loadObj.getTitle();
    

    }

    Image {
    id: myImage

    source: "images/'+ loadObj.getTheme() + "/myPicture.png"
    
    [....]
    

    }
    @

    All the parameters passed transparently throught the loader to the loadable object from the calling application works fine including file names.
    The console error that I get is something like
    @
    Could not find the image "images//myPicture.png"
    @
    just as the theme property is not passed at all. Instead the theme property works if in MyApp.qml I assign explicitly the value to the loadObj.theme property, e.g.

    @
    // Loader component instance
    MyLoader {
    id: loaderInstance

    // Properties assignement
    theme: "xxx"
    title: appaItem.title
    
    [... other stuff ...]
    
    // Absolute document name
    obj: "DocToLoad"
    

    }
    @

    In any other way this particular custom property don't work at all. Using loaders intensively this means to replicate the string instead of using a property.

    Any help is appreciated.

    Cheers :)



  • I don't know if I understood your question correctly.. but did you try aliases rather than normal properties.

    @
    // Properties to customize the loader document
    property string theme: "xxx"
    property string title: "The object title"
    @

    to

    @
    // Properties to customize the loader document
    property alias theme: loaderInstance.them
    property alias title: loaderInstance.title

    Component.onCompleted: {
    // assign the values here...
    }
    @



  • Thank you very much Vijay, it is a good test.

    What it is needed is to define the (theme) property once and apply it in many different loader instances. The alias assignement do the opposite as I have interpreted: you assign a alias for every instance, that is what I avoid. It is possible that I have misunderstood this method ?

    The real solution I missed probably is Component.onCompleted { ... } This can be the reason of the parameter is not passed correctly.


Log in to reply