本页内容

PropertyChanges QML Type

描述状态的新属性绑定或值。更多...

Import Statement: import QtQuick

属性

详细说明

PropertyChanges 用于定义State 中的属性值或绑定。这使得当项目在不同状态之间切换时,其属性值能够随之改变。

要创建一个 PropertyChanges 对象,请像绑定本地属性一样将其绑定到目标项的属性上。这样,您就可以定义新的属性值或绑定。例如:

import QtQuick

Item {
    id: container
    width: 300; height: 300

    Rectangle {
        id: rect
        width: 100; height: 100
        color: "red"

        MouseArea {
           id: mouseArea
           anchors.fill: parent
        }

        states: State {
           name: "resized"; when: mouseArea.pressed
           PropertyChanges {
               rect {
                   color: "blue"
                   height: container.height
               }
           }
        }
    }
}

当鼠标被按下时,Rectangle 将切换至“调整大小”状态。在此状态下,PropertyChanges对象将矩形的颜色设置为蓝色,并将height 的值设置为container.height 的值。

请注意,这会在“resized”状态下自动将rect.height 与container.height 建立绑定。如果不应建立该属性绑定,且仅需将高度设置为状态变化时container.height 的值,请将explicit 属性设置为true 。

PropertyChanges 对象还可以覆盖对象的默认信号处理程序,以实现针对新状态的特定信号处理程序:

PropertyChanges {
    myMouseArea.onClicked: doSomethingDifferent()
}

注意:PropertyChanges 可用于更改锚点边距,但不能更改其他锚点值;若需更改其他锚点值,请改用AnchorChanges 。同样,若要更改Item 的parent 值,请改用ParentChange 。

重置属性值

undefined 值可用于重置某个状态的属性值。在下面的示例中,当myText 切换到widerText状态时,其width 属性会被重置,使文本恢复自然宽度,并将整个字符串显示在单行上。

Rectangle {
    width: 300; height: 200

    Text {
        id: myText
        width: 50
        wrapMode: Text.WordWrap
        text: "a text string that is longer than 50 pixels"

        states: State {
            name: "widerText"
            PropertyChanges { myText.width: undefined }
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: myText.state = "widerText"
    }
}

过渡中的即时属性更改

当使用“过渡”来动画化状态变化时,它们会将属性从当前状态的值动画化至新状态中定义的值(由 PropertyChanges 对象定义)。然而,有时希望在Transition 过程中立即设置属性值,而不进行动画;在这种情况下,可以使用PropertyAction 类型来强制立即更改属性。

更多详细信息请参阅PropertyAction 的文档。

注意: Item 的visible 和enabled 属性与 PropertyChanges 中的其他属性行为并不完全相同。由于这些属性可通过其父项的状态被隐式更改,因此在所有 PropertyChanges 中都应显式设置它们。如果某项的父项之一未被启用或未显示,则该项仍不会被启用或显示。

target 注意:为了与 Qt 5 向后兼容,您也可以使用target 属性以及不带 ID 的普通属性名称来指定 PropertyChanges。例如:PropertyChanges { target: myItem; x: 15 } 。建议使用 ID 形式(如target )而非 。如果文件需通过 Qt Design Studio。请注意, Qt Design Studio 还会对可处理的文件施加一些额外限制。

另请参阅 “States”示例、Qt Quick States以及 Qt Qml。

属性文档

explicit : bool

如果 `explicit` 设置为 `true`,则绑定将被解释为赋值。该表达式仅会被求值一次,即使依赖关系发生变化,也不会进行任何更新。

在下面的示例中,添加 `explicit` 参数可防止 `myItem.width ` 被绑定到 `parent.width`。相反,它会被赋值为状态变更时 `parent.width ` 的值。

PropertyChanges {
    target: myItem
    explicit: true
    width: parent.width
}

默认情况下,explicit 的值为 false。

restoreEntryValues : bool

该属性用于控制在退出该状态时是否应恢复之前的值。

默认值为true 。将此值设置为false 将创建一个临时状态,该状态会对属性值产生持久影响。

target : QtObject

该属性保存了一个对象,该对象包含待修改的属性。

注意: 通常您 无需使用此属性。它仅为兼容 Qt 5 以及 Qt Design Studio。

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