State QML Type
定义对象和属性的配置。更多
Import Statement: | import QtQuick |
属性
详细说明
状态是一组默认配置的批量更改。
所有项目都有一个默认状态,该状态定义了对象和属性值的默认配置。可以通过在states 属性中添加状态项来定义新状态,从而允许项目在不同配置之间切换。例如,这些配置可用于应用不同的属性值集或执行不同的脚本。
下面的示例显示了一个Rectangle 。在默认状态下,矩形显示为黑色。在 "单击 "状态下,PropertyChanges 对象会将矩形的颜色变为红色。在MouseArea 中单击可在默认状态和 "已单击 "状态之间切换矩形的状态,从而在黑色和红色之间切换矩形的颜色。
import QtQuick Rectangle { id: myRect width: 100; height: 100 color: "black" MouseArea { id: mouseArea anchors.fill: parent onClicked: myRect.state == 'clicked' ? myRect.state = "" : myRect.state = 'clicked'; } states: [ State { name: "clicked" PropertyChanges { target: myRect; color: "red" } } ] }
请注意,默认状态使用的是空字符串("")。
状态通常与转场一起使用,以便在状态发生变化时提供动画效果。
注意: 不允许在同一对象的另一个状态中设置该对象的状态。
另请参阅 状态示例、Qt Quick 状态、转场和 Qt Qml.
属性文档
changes : list<Change> |
此属性包含要应用于此状态的更改
默认情况下,这些更改将应用于默认状态。如果状态扩展了另一个状态,则更改将应用于被扩展的状态。
extend : string |
此属性保存此状态扩展的状态。
当一个状态扩展另一个状态时,它会继承该状态的所有更改。
对于扩展状态指定的更改,被扩展的状态被视为基本状态。
name : string |
该属性包含状态的名称。
每个状态在其项目中都应有一个唯一的名称。
when : bool |
该属性表示状态应用的时间。
当需要应用状态时,应将其设置为求值为true
的表达式。例如,当按下MouseArea 时,下面的Rectangle 会进出 "隐藏 "状态:
Rectangle { id: myRect width: 100; height: 100 color: "red" MouseArea { id: mouseArea; anchors.fill: parent } states: State { name: "hidden"; when: mouseArea.pressed PropertyChanges { target: myRect; opacity: 0 } } }
如果一个组中的多个状态都有when
子句,且其评估值同时为true
,则将应用第一个匹配的状态。例如,在以下代码段中,当 sharedCondition 变成true
时,state1
将始终被选中,而不是state2
。
© 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.