While working on my hobby QML Project, I required to detect when particular animation got completed
Well its quite simple to achieve this but initially I struggled with it, I was trying to use onCompleted signal to detect animation end, but latter realised that it comes from component and not from animation.
Actual signal we want to use is onRunningChanged and we can check animation's running property to decide if animation is started or ended. Following is my sample QML code to detect Animation start and stop.
Well its quite simple to achieve this but initially I struggled with it, I was trying to use onCompleted signal to detect animation end, but latter realised that it comes from component and not from animation.
Actual signal we want to use is onRunningChanged and we can check animation's running property to decide if animation is started or ended. Following is my sample QML code to detect Animation start and stop.
Behavior on rotation { PropertyAnimation { id: rotAni; duration: 500 ; onRunningChanged: { if (!rotAni.running) { console.log("Animation completed"); } else { console.log("Animation started"); } } } }As requested in comment, I am posting similar example for NumberAnimation as well.
Rectangle { width: 360 height: 360 gradient:first Gradient { id:first GradientStop { position: 0.0; color: "red" } GradientStop { position: 0.33; color: "yellow" } GradientStop { position: 1.0; color: "green" } } Gradient { id:second GradientStop { position: 0.0; color: "red" } } MouseArea{ anchors.fill:parent onClicked: { parent.gradient=second numAni.running = true; } } NumberAnimation{ target:smallRect id:numAni properties: "x"; to: 100; duration: 1000 onRunningChanged:{ if (!numAni.running) { console.log("numAni Animation completed"); } else { console.log("numAni Animation started"); } } } Rectangle{ id:smallRect width:10; height:10; } }