父子关系中Window和Item之间QML的差异 [英] Difference in QML between Window and Item in parent-children relationship

查看:29
本文介绍了父子关系中Window和Item之间QML的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么 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.

推荐答案

根据documentationanchors.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:

  • Windowid 不能识别 Item
    → 通过 id 锚定会导致 错误
  • parent 实际上是隐藏的 Item 派生的 contentItemWindow 的每个子项都自动成为其父项
    parent 锚定正确工作
  • the id of the Window does not identify an Item
    → anchoring by id results in an error
  • the parent is actually the hidden Item-derived contentItem to which each child of Window is automatically parented
    → anchoring by parent correctly works

这篇关于父子关系中Window和Item之间QML的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆