本页内容

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" }
}

红色方块在两种状态之间切换锚点位置

AnchorChanges 可以通过AnchorAnimation 实现动画效果。

//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 。

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