【问题标题】:Difference in QML between Window and Item in parent-children relationship父子关系中Window和Item的QML差异
【发布时间】:2015-12-30 09:55:20
【问题描述】:

我想知道为什么 Item 会这样:

Item {
    id: root
    width: 640
    height: 480
    MouseArea {
        anchors.fill: (root or parent. It doesn't matter)
        onClicked: console.log("clicked")
    }
}

但对于Window 却没有。只有通过父锚定有效,但通过 id 锚定会失败。

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    根据documentationanchors.fill 要求参数是或标识Item 派生对象。

    here你可以沿着Window的继承链看它实际上不是Item
    而且,从here你可以看到:

    如果您将一个 Item 分配给数据列表,它会成为 Window 的 contentItem 的子项,因此它会出现在窗口内。

    contentItem 的位置:

    此附加属性包含 场景的不可见根项目,如果项目不在窗口中,则为 null。

    因此,您所观察的内容是有意义的:

    • Windowid 不能识别 Item
      → 通过id 锚定会导致错误
    • parent 实际上是隐藏的Item 派生的contentItemWindow 的每个孩子都自动成为其父
      → 由parent 正确锚定工作 李>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多