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 的值。

请注意,这会自动将rect.height调整大小状态下的container.height 绑定。如果不需要建立属性绑定,而只需在状态改变时将高度设置为container.height 的值,则可将explicit 属性设置为true

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

PropertyChanges {
    myMouseArea.onClicked: doSomethingDifferent()
}

注意: PropertyChanges 可用于更改锚点边距,但不能用于更改其他锚点值;请使用AnchorChanges 代替。同样,要更改Itemparent 值,请使用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 文档。

注意: Itemvisibleenabled 属性的行为与 PropertyChanges 中的其他属性并不完全相同。由于这些属性可以通过父属性的状态隐式更改,因此应在所有 PropertyChanges 中明确设置这些属性。如果项目的父级中有一个未启用或未可见,则该项目仍不会启用或可见。

注意: 为了向后兼容 Qt 5,您也可以使用target 属性和不带 ID 的普通属性名称指定 PropertyChanges。例如:PropertyChanges { target: myItem; x: 15 } 。建议使用带 ID 的表单,而不是target 。如果文件要使用target 编辑,您可能还需要使用带 Qt Design Studio.注意 Qt Design Studio还对其可处理的文件施加了一些限制。

另请参阅 "国家 "示例Qt Quick "国家 "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.


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