Loader child element



  • Hi,

    I try to access a loader and assign a source file dynamically using JavaScript. That works fine so far.
    But now I need to set some properties of the item I loaded to the loader element.

    The problem is I don't know how to access the element that was instantiated by the loader.
    Is there some kind of child or whatever property that I can refer to.

    Thanks for help guys



  • Oh no thats embarrassing.

    A simple case of reading the docs and everything is fine.

    I didn't read the Loader docs properly.
    If I had I had seen the Item property which holds the instance of the loaded item.

    Sorry for that guys.



  • Oh boy...

    Ok another problem occurred.

    I have the following procedure.

    The qml item which should be instantiated.
    @import QtQuick 1.0
    import Product 0.1

    Item{
    id:detail_screen
    anchors.fill: parent

    property alias product: m_product
    

    Text {
    id: name
    anchors.fill: parent
    text: m_product.productName
    }

    Product{
    id: m_product
    productName: "foo"

    }
    
    Component.onCompleted:
    

    console.log(product.productName);

    }@

    and the JavaScript function which do that for me.
    @
    function loadDetailScreen(source,product)
    {
    console.log(source);
    select_loader.source = source; //load the qml file shown above
    if(select_loader.status == Loader.Ready)
    {
    select_loader.item.product.productName = "bar";
    }
    console.log(select_loader.item.product.productName); //that actually shows "bar"
    }
    @

    When the loadDetailScreen() function is called the loader instantiates the qml item
    which initially has a productName "foo". But I wanna set this productName after loading to "bar"
    This works pretty well so far. But on screen appears still "foo" as text.

    I tried to set a string property which takes the productName and this worked, but I wanna store these informations directly in the product child item.

    It seems like that the change on the productName property won't be synchronized with the text, which has to display it.

    What could be the problem here ?
    Why does the item show still "foo" although I set it to "bar" after loading ?

    Any ideas ?



  • I tried running your code as a standalone example and it worked fine - the Text item does indeed update to display "foo" instead of "bar". Perhaps there is something else in your code that is changing this property after loadDetailScreen() has finished?



  • Hi blam

    the text item is actually supposed to display "bar" after loadDetailScreen() has finished.
    "foo" is the initial value for the text and should be updated to "bar". Which doesn't seem to happen.



  • Sorry, my mistake - I meant to write, it does indeed change to "bar" instead of "foo".

    This is the code that I'm trying:

    • SomeItem.qml
      @

    import QtQuick 1.0

    Item {
    property alias product: m_product

    Text { text: m_product.productName }
    
    Item {
        id: m_product
        property string productName: "foo"
    
    }
    

    }
    @

    • main.qml
      @
      import QtQuick 1.0

    Item {
    width: 400; height: 400

    Loader { id: select_loader }
    
    Component.onCompleted: loadDetailScreen("SomeItem.qml")
    
    function loadDetailScreen(source)
    {
        console.log(source);
        select_loader.source = source; //load the qml file shown above
        if(select_loader.status == Loader.Ready)
        {
            select_loader.item.product.productName = "bar";
        }
        console.log(select_loader.item.product.productName); //that actually shows "bar"
    }
    

    }
    @


Log in to reply
 

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