Qt 改变动画目标
无法将动画从一个对象切换到另一个对象。id会更改(它在日志中打印“world”,但不会传输动画:Qt 改变动画目标,qt,qml,qtquick2,Qt,Qml,Qtquick2,无法将动画从一个对象切换到另一个对象。id会更改(它在日志中打印“world”,但不会传输动画:hello仍在闪烁,并且world是静态的 只有在调用a.restart()时,它才能正常工作。当没有函数,只有绑定时,可以使用onChanged并控制动画如何停止(完成或暂停)if(运行){complete();restart();} import QtQuick 2.5 Column { ColorAnimation { id: a target: la
hello
仍在闪烁,并且world
是静态的
只有在调用a.restart()
时,它才能正常工作。当没有函数,只有绑定时,可以使用onChanged
并控制动画如何停止(完成或暂停)if(运行){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"
}
}
在更改目标之前,应停止动画:
a.running = false
a.target = lab2
a.running = true
它对我来说很好现在我将使用它(刚刚添加了onTargetChanged):
和绑定(按下时动画切换到另一个标签):
这看起来像一个bug(或者至少是一个文档问题),所以在bugreports.qt.io上提交一个bug报告。在绑定中不可能这样做。你是什么意思?您只需用我的代码替换
a.target=lab2
,就可以了。没有单击onClick
将代码粘贴到此处。而我的实际项目也没有点击onClick
。
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"
}
}
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"
}
}