changing property of element from other qml file

2019-09-09 16:37发布

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 to change property of

ei.itemUrl

from

View2.qml

in working way.

1条回答
姐就是有狂的资本
2楼-- · 2019-09-09 17:13

The ei element won't be available directly in other QML file. You can use an alias to do it.

property alias callUrl: ei.itemUrl

and call it from other QML file

luncherList.callUrl='file:///home/user/file.jpg'
查看更多
登录 后发表回答