AnchorChanges QML Type

指定如何更改状态中项目的锚点。更多

Import Statement: import QtQuick

属性

详细说明

AnchorChanges 类型用于修改State 中项目的锚点。

AnchorChanges 不能用于修改项目的边距。为此,请使用PropertyChanges

在下面的示例中,我们使用 AnchorChanges 更改项目的顶部和底部锚点,并使用PropertyChanges 更改顶部和底部锚点的边距:

import QtQuick

Rectangle {
    id: window
    width: 120; height: 120
    color: "black"

    Rectangle { id: myRect; width: 50; height: 50; color: "red" }

    states: State {
        name: "reanchored"

        AnchorChanges {
            target: myRect
            anchors.top: window.top
            anchors.bottom: window.bottom
        }
        PropertyChanges {
            target: myRect
            anchors.topMargin: 10
            anchors.bottomMargin: 10
        }
    }

    MouseArea { anchors.fill: parent; onClicked: window.state = "reanchored" }
}

使用AnchorAnimation 可以使 AnchorChanges 变为动画。

//animate our anchor changes
Transition {
    AnchorAnimation {}
}

锚点边距的更改可以通过NumberAnimation 动画显示。

有关锚点的更多信息,请参阅锚点布局

属性文档

anchors group

anchors.baseline : AnchorLine

anchors.bottom : AnchorLine

anchors.horizontalCenter : AnchorLine

anchors.left : AnchorLine

anchors.right : AnchorLine

anchors.top : AnchorLine

anchors.verticalCenter : AnchorLine

这些属性可更改项目的相应锚点。

要重置锚点,可以指定undefined

AnchorChanges {
    target: myItem
    anchors.left: undefined          //remove myItem's left anchor
    anchors.right: otherItem.right
}

target : Item

该属性包含将应用锚点更改的Item


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