LayoutMirroring QML Type (Uncreatable)
用于反映布局行为的属性。更多...
| Import Statement: | import QtQuick |
注意:这是一个不可创建的类型。无法在 QML 中对其进行实例化。
属性
- childrenInherit : bool
- enabled : bool
详细说明
LayoutMirroring 附加类型用于水平镜像项锚点、定位器类型(如Row 和Grid )以及视图(如GridView 和水平ListView )。镜像是一种视觉上的变化:左锚点变为右锚点,而像Grid 和Row 这样的定位器类型会反转子项的水平布局。
要启用某项的镜像效果,需将enabled 属性设置为true。默认情况下,这仅影响该项本身;将childrenInherit 属性设置为true可将镜像行为传播至所有子项。如果某项未定义LayoutMirroring 附加类型,则不会启用镜像效果。
注意:自 Qt 5.8起 ,LayoutMirroring 可以附加到Window 上。实际上,这与将LayoutMirroring 附加到窗口的contentItem 上效果相同。
以下示例展示了镜像效果的实际应用。下方的Row 被指定为锚定在其父窗口的左侧。然而,由于已启用镜像功能,锚点在水平方向上被反转,现在锚定在右侧。 此外,由于Row 中的项目默认从左到右排列,现在它们的排列顺序已变为从右到左,这可通过项目的编号和不透明度来体现:
import QtQuick
Rectangle {
LayoutMirroring.enabled: true
LayoutMirroring.childrenInherit: true
width: 300; height: 50
color: "yellow"
border.width: 1
Row {
anchors { left: parent.left; margins: 5 }
y: 5; spacing: 5
Repeater {
model: 5
Rectangle {
color: "red"
opacity: (5 - index) / 5
width: 40; height: 40
Text {
text: index + 1
anchors.centerIn: parent
}
}
}
}
}
当需要同时支持应用程序的从左到右和从右到左两种布局版本以面向不同的语言区域时,布局镜像功能非常有用。childrenInherit 属性允许应用布局镜像,而无需为应用程序中的每个项目手动设置布局配置。 但请注意,镜像功能不会影响由 `Item `(x )坐标值定义的任何定位,因此即使启用了镜像功能,通常仍需进行一些布局调整以支持所需的布局方向。 此外,如果镜像功能并非所需的行为,或者子项已经以某种自定义方式实现了镜像功能,则可能需要禁用单个子项的镜像功能(通过将这些项的LayoutMirroring.enabled 设置为false)。
要根据应用程序的默认布局方向设置布局方向,请使用以下代码:
有关如何使用LayoutMirroring 及其他相关功能为应用程序实现从右到左支持的更多详细信息,请参阅《从右到左用户界面》。
属性文档
childrenInherit : bool
此属性控制该项的LayoutMirroring.enabled 值是否由其子项继承。
默认值为 false。
enabled : bool
该属性控制项目布局是否进行水平镜像。将其设置为 true 时,将反转锚点设置,即左锚点变为右锚点,右锚点变为左锚点。对于定位器类型(如Row 和Grid )以及视图类型(如GridView 和ListView ),这也会使项目的水平布局方向发生镜像。
默认值为 false。
© 2026 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.