自定义组件中是可以直接使用main界面中根节点这个层级中声明的所有id
MyRect01.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
Rectangle{id:rec01width:450height: 200color: "green"Button{id:btn01text: "dian ji"onClicked: {yRec01.visible=falseyRec02.visible=true}}}
MyRec02.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
Rectangle{id:rec01width: 300height: 150x:350color: "red"Button{id:btn01text: "dian ji"onClicked: {yRec01.visible=trueyRec02.visible=false}}
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
Window {visible: truewidth: 640height: 480title: qsTr("Hello World")MyRec01{id:yRec01visible: true}MyRec02{id:yRec02visible: false}}
运行结果:
FR:徐海涛(hunkxu)