Window与Item在父子关系中QML的区别
Difference in QML between Window and Item in parent-children relationship
我想知道为什么 Item
这行得通:
Item {
id: root
width: 640
height: 480
MouseArea {
anchors.fill: (root or parent. It doesn't matter)
onClicked: console.log("clicked")
}
}
但对于 Window
则不然。只有通过父级锚定才有效,但通过 id 锚定将失败。
根据 documentation,anchors.fill
要求参数是或标识一个 Item
派生的 object。
从 here 开始,您可以跟踪 Window
的继承链,发现它实际上不是 Item
。
而且,从here可以看出:
If you assign an Item to the data list, it becomes a child of the Window's contentItem, so that it appears inside the window.
contentItem
我们有:
This attached property holds the invisible root item of the scene or null if the item is not in a window.
正因为如此,您所观察到的才有意义:
Window
的 id
没有识别出 Item
→ id
锚定导致 错误
parent
实际上是隐藏的 Item
派生的 contentItem
,Window
的每个 child 都自动成为父
→通过 parent
正确锚定 有效
我想知道为什么 Item
这行得通:
Item {
id: root
width: 640
height: 480
MouseArea {
anchors.fill: (root or parent. It doesn't matter)
onClicked: console.log("clicked")
}
}
但对于 Window
则不然。只有通过父级锚定才有效,但通过 id 锚定将失败。
根据 documentation,anchors.fill
要求参数是或标识一个 Item
派生的 object。
从 here 开始,您可以跟踪 Window
的继承链,发现它实际上不是 Item
。
而且,从here可以看出:
If you assign an Item to the data list, it becomes a child of the Window's contentItem, so that it appears inside the window.
contentItem
我们有:
This attached property holds the invisible root item of the scene or null if the item is not in a window.
正因为如此,您所观察到的才有意义:
Window
的id
没有识别出Item
→id
锚定导致 错误parent
实际上是隐藏的Item
派生的contentItem
,Window
的每个 child 都自动成为父
→通过parent
正确锚定 有效