[SOLVED] changing property of element from other qml file



  • Hi,
    I know that there is tons of topic similar like this, I try to implement answer from them and I still have no results.

    I take some sample project from qt creator to play with this. I play with changing visibility of qml files ( treat every file as other screen). After lunching 3rd screen I want to make the second one invisible.
    Here Is the code where I want change property in it:
    MyLuncherList.qml

    import QtQuick 2.0

    @Rectangle {

    Item
    {
        id:ei
        visible:false
        clip: true
    
        property url itemUrl
         onItemUrlChanged:
        {
    
            visible = (itemUrl== '' ? false : true);
        }
        anchors.fill: parent
        anchors.bottomMargin: 40
    
         Rectangle
        {
            id:bg
            anchors.fill: parent
            color: "white"
        }
        MouseArea
        {
             anchors.fill: parent
             enabled: ei.visible
             //takes mouse events
        }
        Loader
        {
            focus:true
            source: ei.itemUrl
            anchors.fill: parent
        }
    }
    

    }@
    and here is the code where I want to make a action

    View2.qml
    @
    import QtQuick 2.0

    Rectangle {
    width: 100
    height: 62
    Text
    {
    text: "second screen"
    }
    MyLuncherList
    {
    id:luncherList
    }
    Rectangle
    {
    x: 50
    y: 30
    width: 120
    height: 60
    color: "red"
    MouseArea
    {
    anchors.fill: parent
    id: mouseAreaWhichHides
    onClicked:
    {
    luncherList.ei.itemUrl = '';
    }
    }
    }

    }@
    and I got the error qrc:///View2.qml:29: TypeError: Type error
    which point on this line @luncherList.ei.itemUrl = '';@
    Type error says that I make some mismatch with Type, but I'm not even sure, if I do this access process in properly way, so I'm asking how the access to @ei.itemUrl@ from @View2.qml@ should be done.


  • Moderators

    Hi,

    You can do it in 2 ways

    1. Using alias

    @
    Rectangle {
    property alias myurl: ei.itemUrl
    Item
    {
    id:ei
    visible:false
    clip: true
    property url itemUrl
    ...
    @

    and access it as

    @
    luncherList.myurl
    @

    1. Get the exact item using children (Not recommended)

    @
    luncherList.children[0].itemUrl
    @



  • The first option apparently works cause I don't get any errors,but I have another question. If I put into ei.itemUrl = '' the method onItemUrlChanged: should be invoked, but it isn't. I need to do other actions to refresh it or what?


  • Moderators

    The handler didn't got fired since there was no change. See your itemUrl is blank("") initially and again your are assigning it a blank, so no change.



  • Starting from the base, I start app and MyLuncherList.qml appears, then I'm putting properly value into ei.itemUrl, so right now the value isn't empty and it loads second screen, then I use similar method in second screen to load 3rd screen. In 3rd screen I want to make second screen invisible by this code which I show above. So now if I change the property it would fire or not. If I access the property which was already changed or I just by this code
    @MyLuncherList
    {

        id:luncherList
        visible: false
    }@
    

    create new instance of the qml file, I m not sure how to named it properly


  • Moderators

    I don't know about your other code. Atleast in the above code i dont see where you are assigning a value to itemUrl other than in View2.qml. AFAIK the on<property_name>Changed signal handler are executed when a change is detected.


Log in to reply
 

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