LayoutMirroring QML Type

用于反映布局行为的属性。更多

Import Statement: import QtQuick

属性

详细说明

LayoutMirroring 附加属性用于水平镜像项目锚点定位器类型(如RowGrid )和视图(如GridView 和水平ListView )。镜像是一种视觉变化:左边的锚点变成右边的锚点,定位器类型(如GridRow )反转子项目的水平布局。

通过将enabled 属性设置为 true,可以启用项目的镜像功能。默认情况下,这只会影响项目本身;将childrenInherit 属性设置为 true 后,镜像行为也会传播到所有子项目。如果未为项目定义LayoutMirroring attached 属性,则不会启用镜像。

注: 自 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.enabled: Qt.application.layoutDirection === Qt.RightToLeft

有关使用LayoutMirroring 和其他相关功能为应用程序实现从右向左支持的更多详情,请参阅从右向左用户界面

属性文档

childrenInherit : bool

该属性表示LayoutMirroring.enabled 的值是否由其子项继承。

默认值为 false。


enabled : bool

该属性显示项目的布局是否水平镜像。将其设置为 true 时,锚点设置会水平反转,左锚点变为右锚点,右锚点变为左锚点。对于定位器类型(如RowGrid )和视图类型(如GridViewListView ),该值也会镜像项目的水平布局方向。

默认值为 false。


© 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.