1. 程式人生 >實用技巧 >其它 >Qt之QSequentialAnimationGroup

Qt之QSequentialAnimationGroup


技術標籤:qtiosmanjaropyqt

#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);
}

Qt之QSequentialAnimationGroup

Qt之QSequentialAnimationGroup

任意一個按鈕點選都都會移動按鈕,先移動按鈕1,再移動按鈕2

Qt之QSequentialAnimationGroup

Qt之QSequentialAnimationGroup

Qt之QSequentialAnimationGroup

任意一個按鈕點選都都會移動按鈕:按鈕1&按鈕2並行移動

Qt之QSequentialAnimationGroup

序列順序執行動畫物件