Qt之QSequentialAnimationGroup

#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並行移動this

 

串行順序運行動畫對象code

相關文章
相關標籤/搜索