LayoutItemProxy QML Type

LayoutItemProxy 类为布局中的QQuickItems 提供了一个占位符。更多

Import Statement: import QtQuick.Layouts
Since: QtQuick.Layouts 6.6
Inherits:

Item

属性

详细说明

一些响应式布局需要针对不同的屏幕尺寸采用不同的布局层次,但布局层次与 QML Runtime 结构相同,因此无法在运行时更改。LayoutItemProxy 通过表示布局中的target 项目,克服了这一限制。target 项目本身可定义在 QML 层次结构的任何位置。这样就可以用相同的内容项声明多个布局。布局可以显示和隐藏,以便在它们之间切换。

如果target 项目是visible ,LayoutItemProxy 就会尝试控制它。取得控制权后,target 项目的位置和大小将与 LayoutItemProxy 的位置和大小相匹配。此外,LayoutItemProxy 会将自己设置为target 的父级(以确保事件传递和有用的绘制顺序),并将可见性设置为true 。多个 LayoutItemProxy 可以target 同一项目,但一次只能有一个 LayoutItemProxy 控制一个项目。因此,一次只能有一个针对同一项目的代理可见。如果多个代理针对同一项目,但每个代理的visible都设置为 false,那么该项目也将不可见。

LayoutItemProxy 会转发target 的所有Layout 附加属性,以及targetQQuickItem::implicitWidthQQuickItem::implicitHeight 附加属性。LayoutItemProxy 将在Layout 属性和大小方面尽可能模仿target 。也可以在 LayoutItemProxy 上显式设置Layout 附加属性,这将停止转发target 属性。

使用示例

这是一个最简单的示例,使用代理在两个布局之间切换,以便在两个布局中使用相同的项目。填充布局的项目可在 QML 结构的任意位置定义。

Rectangle {
    id: rectangle1
    color: "tomato"
    Layout.fillHeight: true
    Layout.fillWidth: true
}

Rectangle {
    id: rectangle2
    color: "lightskyblue"
    Layout.fillHeight: true
    Layout.fillWidth: true
}

然后,我们可以使用 LayoutItemProxys 来定义布局。

GridLayout {
    id: l1
    columns: 1
    visible: false
    anchors.fill: parent
    LayoutItemProxy { target: rectangle1 }
    LayoutItemProxy { target: rectangle2 }
}

GridLayout {
    id: l2
    columns: 2
    visible: true
    anchors.fill: parent
    LayoutItemProxy { target: rectangle1 }
    LayoutItemProxy { target: rectangle2 }
}

现在,我们可以根据自己选择的标准,通过切换布局的可见性在布局之间进行切换。

onWidthChanged: {
    if (width < 300) {
        l2.visible = false
        l1.visible = true
    } else {
        l1.visible = false
        l2.visible = true
    }
}

由此产生的两种布局看起来就像这样:

LayoutItemProxy 也可以在没有布局的情况下使用,例如将其锚定到不同的项目上。同样也可以混合使用Items 和代理项,以及布局和项的嵌套结构。

警告: LayoutItemProxy 会将其目标的父级设置为自身。在引用目标项的父项时,请牢记这一点。

另请参阅 Item,GridLayout,RowLayout, 和ColumnLayout

属性文档

target : Item

该属性保存了Item ,该代理应在Layout 层次结构中代表该 。


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.