我正在尝试实施一个健身应用程序,显示当前的锻炼名称,然后在休息期间显示进度条.当进度条完全填满时,将显示下一个练习的名称,然后在完成时显示进度条,依此类推.
请注意,我在此示例中使用了一个进度条,但我将在实际应用程序中拥有自己的小部件.
我的系统有两种状态:
所以它是这样的:
我的问题在于第5步:我不知道如何在转换结束时切换回另一个状态.我试图在PropertyChanges期间更改"state"属性,并在使用SequentialAnimation的转换结束时计时,但是我收到此错误消息:
QML StateGroup:不能将状态更改作为状态定义的一部分应用.
以下是一些示例代码:
import QtQuick 2.0
import QtQuick.Controls 1.1
ProgressBar {
id: root
width: 200
height: 48
minimumValue: 0
maximumValue: 100
value: 76
function switchState() {
if (state == "exercise")
{
state = "rest"
return
}
if (state == "rest")
{
state = "exercise"
return
}
}
state: "exercise"
states: [
State {
name: "exercise"
PropertyChanges {
target: root
value: 0
}
},
State {
name: "rest"
PropertyChanges {
target: root
value: maximumValue
// error: QML StateGroup: Can't apply a state change as part of a state definition.
// state: "exercise"
}
}
]
transitions: [
Transition {
to: "rest"
PropertyAnimation {
target: root
properties: "value"
duration: 1000
}
}
]
MouseArea {
anchors.fill: parent
onClicked: parent.switchState()
}
}
Run Code Online (Sandbox Code Playgroud)
如何在状态转换结束时切换到另一个状态?
我相信你可以通过使用转换中的RunningChanged信号来做到这一点:
transitions: [
Transition {
to: "rest"
PropertyAnimation {
target: root
properties: "value"
duration: 1000
}
onRunningChanged: {
if ((state == "rest") && (!running))
switchState();
}
}
]
Run Code Online (Sandbox Code Playgroud)
QML对象具有属性更改的相关信号(on<Property>Changed).关联的处理程序通常不会记录在对象的引用中,但由于属性的存在而隐式可用.看到这个.