Change the purpose of the animation

Cannot switch animation from one object to another. The identifier changes (it prints "world" in the log), but it does not transmit animation: hello is still blinking, and world static.

It only works correctly when a.restart() called. When there are no functions, just bindings, you can use onChanged and control how the animation stops (ends or pauses) if (running) { complete(); restart(); } if (running) { complete(); restart(); } if (running) { complete(); restart(); } .

 import QtQuick 2.5 Column { ColorAnimation { id: a target: lab1 property: "color" running: true loops: Animation.Infinite duration: 500 from: "black" to: "red" } Text { id: lab1 text: "hello" MouseArea { anchors.fill: parent onClicked: { a.target = lab2 console.log("changed") console.log(a.target.text) } } } Text { id: lab2 text: "world" } } 
+6
source share
2 answers

I will use this now (just added onTargetChanged):

 import QtQuick 2.5 Column { ColorAnimation { id: a target: lab1 onTargetChanged: { if (running) { complete(); restart(); } } property: "color" running: true loops: Animation.Infinite duration: 500 from: "black" to: "red" } Text { id: lab1 text: "hello" MouseArea { anchors.fill: parent onClicked: { a.target = lab2 console.log("changed") console.log(a.target.text) } } } Text { id: lab2 text: "world" } } 

And with binding (animation switches to another label when clicked):

 import QtQuick 2.5 Column { id: root ColorAnimation { id: a target: ma.pressed ? lab2 : lab1 onTargetChanged: { if (running) { complete(); restart(); } } property: "color" running: true loops: Animation.Infinite duration: 500 from: "black" to: "red" } Text { id: lab1 text: "hello" MouseArea { id: ma anchors.fill: parent } } Text { id: lab2 text: "world" } } 
+1
source

You must stop the animation before changing the target:

 a.running = false a.target = lab2 a.running = true 

This works great for me.

+1
source

All Articles