父子关系中Window和Item之间QML的差异 [英] Difference in QML between Window and Item in parent-children relationship
问题描述
我想知道为什么 Item
会这样:
I'm wondering why for Item
this works:
Item {
id: root
width: 640
height: 480
MouseArea {
anchors.fill: (root or parent. It doesn't matter)
onClicked: console.log("clicked")
}
}
但对于 Window
则不然.只有父级锚定才有效,但 id 锚定会失败.
But for Window
it doesn't. Only anchoring by parent will work, but for anchoring by id it will fail.
推荐答案
根据documentation,anchors.fill
要求参数是或标识 Item
派生的对象.
According to the documentation, anchors.fill
requires the argument either to be or to identify an Item
-derived object.
从这里,您可以遵循继承链Window
并看到它实际上不是一个 Item
.
此外,从这里你可以看到那个:
From here you can follow the inheritance chain of Window
and see that it is not actually an Item
.
Moreover, from here you can see that:
如果您将一个 Item 分配给数据列表,它会成为 Window 的 contentItem 的子项,以便它出现在窗口内.
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
,我们有:
此附加属性保存场景的不可见根项目,如果项目不在窗口中,则为 null.
This attached property holds the invisible root item of the scene or null if the item is not in a window.
因此,您所观察到的内容是有道理的:
Because of that, it makes sense what you are observing:
Window
的id
不能识别Item
→ 通过id
锚定会导致 错误parent
实际上是隐藏的Item
派生的contentItem
,Window
的每个子项都自动成为其父项
→parent
锚定正确工作
- the
id
of theWindow
does not identify anItem
→ anchoring byid
results in an error - the
parent
is actually the hiddenItem
-derivedcontentItem
to which each child ofWindow
is automatically parented
→ anchoring byparent
correctly works
这篇关于父子关系中Window和Item之间QML的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!