首页 > 解决方案 > QML RotationAnimation 不旋转

问题描述

我正在尝试将矩形从指定角度旋转到指定角度,但我不确定我是否理解文档。我下面的代码运行,我开始和完成的插槽打印正确的角度。但是矩形没有在屏幕上旋转。我错过了什么?

Rectangle {
  width: 100
  height: 100
   RotationAnimation {
        id: rotateCritter
        duration: 1000
        property real lastAngle: 0
        onStarted: {
            lastAngle = to;
            console.log("Rotating from "+from+" to "+to)
        }
        onStopped: {
            console.log("Done rotating from "+from+" to "+to)
            from = lastAngle;
        }
    }
}


// On click totate the critter to the new angle
rotateCritter.to =  45
rotateCritter.start()

标签: qtanimationrotationqml

解决方案


RotationAnimation缺少一个target. 虽然它是 的孩子,但Rectangle这种关系不会自动Rectangle生成target动画的 ;它必须是明确的。我已经给出了Rectangle一个idand color,并把它变成target了动画的:

    Rectangle {
        id: critter
        width: 100
        height: 100
        color: "red"

        RotationAnimation {
            id: rotateCritter
            target: critter
            duration: 1000
            property real lastAngle: 0
            onStarted: {
                lastAngle = to;
                console.log("Rotating from "+from+" to "+to)
            }
            onStopped: {
                console.log("Done rotating from "+from+" to "+to)
                from = lastAngle;
            }
        }
    }


推荐阅读