最简单的动画实现就是为QObject(某个东西)创建一个QPropertyAnimation(属性动画)。然后为动画设置持续时间、初始值和最终值等参数。看代码后面的解释。
QWidget* w = new QWidget(this);
//假设this是一个QWidget。在一个Widget中创建一个小Widget
QPropertyAnimation* animation = new QPropertyAnimation(w,"pos",this);
//创建一个属性动画, 动画的目标是w 属性是位置
animation->setDuration(10000);//设置持续时间为10s
animation->setEasingCurve(QEasingCurve::InOutBack);
//设置插值方法,通俗的说是设置动画效果。
animation->setKeyValueAt(0,QPoint(0,0));
//插值,值是0 - 1.0之间0值也可以用setStartValue()
animation->setKeyValueAt(1.0,QPoint(100,100));
animation->start(QAbstractAnimation::DeleteWhenStopped);
//启动动画,参数是动画删除策略。默认是KeepWhenStopped(结束时不删除)。