#include "widget.h"
#include <QPushButton>
#include <QPropertyAnimation>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(300, 200);
QPushButton *btn1 = new QPushButton("first", this);
btn1->setGeometry(10, 10, 100, 30);
QPushButton *btn2 = new QPushButton("second", this);
btn2->setGeometry(10, 45, 100, 30);
QPropertyAnimation *anim1 = new QPropertyAnimation(btn1, "geometry");
anim1->setDuration(2000);
anim1->setStartValue(QRectF(10, 10, 100, 30));
anim1->setEndValue(QRectF(200, 150, 100, 30));
QPropertyAnimation *anim2 = new QPropertyAnimation(btn2, "geometry");
anim2->setDuration(2000);
anim2->setStartValue(QRect(10, 45, 100, 30));
anim2->setEndValue(QRect(200, 195, 100, 30));
// sGroup = new QSequentialAnimationGroup; // Sequential Group
// sGroup->addAnimation(anim1);
// sGroup->addAnimation(anim2);
pGroup = new QParallelAnimationGroup; // Parallel Group
pGroup->addAnimation(anim1);
pGroup->addAnimation(anim2);
connect(btn1, SIGNAL(clicked()), this, SLOT(btn_clicked()));
connect(btn2, SIGNAL(clicked()), this, SLOT(btn_clicked()));
}
void Widget::btn_clicked()
{
// sGroup->start(QPropertyAnimation::DeleteWhenStopped); //如果动画完成自动清理内存
//因此移动一次之后就算再次点击按钮也不会移动了
pGroup->start(QPropertyAnimation::DeleteWhenStopped);
}
任意一个按钮点击都都会移动按钮,先移动按钮1,再移动按钮2
任意一个按钮点击都都会移动按钮:按钮1&按钮2并行移动
串行顺序运行动画对象