本页内容

ParentChange QML Type

指定在状态变化时如何为项目重新设置父项。更多...

Import Statement: import QtQuick

属性

详细说明

ParentChange 在重新关联项目父节点的同时,会保留其在屏幕上的视觉外观(位置、大小、旋转和缩放)。随后,您可以指定一个过渡效果,将该项目移动、调整大小、旋转或缩放至最终预期外观。

只有当不涉及复杂变换时,ParentChange 才能保留视觉外观。更具体地说,如果参与重新归属的任何项目(即原始父项和新父项的共同祖先树中的项目)设置了 transform 属性,则该功能将无法正常工作。

下面的示例并排显示了一个大的红色矩形和一个小的蓝色矩形。当点击blueRect 时,它将切换到“重新关联父级”状态:其父级变为redRect ,并且根据ParentChange中的指定,在红色矩形内定位于(10, 10)处。

import QtQuick

Item {
    width: 200; height: 100

    Rectangle {
        id: redRect
        width: 100; height: 100
        color: "red"
    }

    Rectangle {
        id: blueRect
        x: redRect.width
        width: 50; height: 50
        color: "blue"

        states: State {
            name: "reparented"
            ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
        }

        MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
    }
}

蓝色方块的状态变化会影响父节点的重新分配

您可以通过使用ParentAnimation 来指定希望在过渡的哪个时间点触发ParentChange。

请注意,与 `PropertyChanges` 不同,`ParentChange` 要求目标为基于 `Item` 的对象;它无法与任意对象配合使用(例如,您无法使用它来为 `Timer` 重新设置父级)。

属性文档

height : real

rotation : real

scale : real

width : real

x : real

y : real

这些属性保存了该项目在此状态下的新位置、大小、缩放比例和旋转角度。

parent : Item

该属性存储了处于此状态的项的新父项。

target : 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.