State(状态)组件是一组来自默认配置的批处理更改。所有项都有一个默认状态,该状态定义对象和属性值的默认配置。可以通过将State项添加到states属性来定义新的状态,以允许项在不同的配置之间切换。
State组件的基本用法如下:
Window {
id: win
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
id: rect
anchors.fill: parent
color: "steelblue"
state: "initial"
MouseArea {
id: ma
anchors.fill: parent
hoverEnabled: true
onClicked: {
rect.state === 'clicked'? rect.state="":rect.state='clicked'
}
onEntered: {
rect.state = "enter"
}
onExited: {
rect.state = 'initial'
}
}
states: [
State {
name: "initial"
PropertyChanges {
target: rect
color: "steelblue"
}
},
State {
name: "clicked"
PropertyChanges {
target: rect
color: "re"
}
},
State {
name: "enter"
PropertyChanges {
target: rect
color: "green"
}
}
]
}
}