Qpropertyanimation并不能保存小部件的最后一个声音



我正在尝试使用多个qpropertyanimation将小部件移动,然后向右移动一个小部件。

这是代码:

QPropertyAnimation* animationUp;
QPropertyAnimation* animationRight;
QSequentialAnimationGroup *group;
animationUp = new QPropertyAnimation(this->ui->pushButton_2, "geometry");
animationUp->setStartValue(QRect(this->ui->pushButton_2->pos().x(), this->ui->pushButton_2->pos().y(), buttonWidth, buttonHeight));
animationUp->setEndValue(QRect(this->ui->pushButton_2->pos().x(), this->ui->pushButton_2->pos().y() - 60, buttonWidth, buttonHeight));
animationRight = new QPropertyAnimation(this->ui->pushButton_2, "geometry");
animationRight->setStartValue(QRect(this->ui->pushButton_2->pos().x(), this->ui->pushButton_2->pos().y(), buttonWidth, buttonHeight));
animationRight->setEndValue(QRect(this->ui->pushButton_2->pos().x() + 60, this->ui->pushButton_2->pos().y(), buttonWidth, buttonHeight));
group = new QSequentialAnimationGroup;
group->addAnimation(animationUp);
group->addAnimation(animationRight);
group->start();

问题很简单,小部件将向上移动,然后向右移动,但要从起始位置移动,而不是从向上移动后的位置移动。我希望它从最后一个位置,而不是从开始位置向上移动。

编辑:

@eyllanesc的答案在我问的问题上工作正常,但是我应该在打算做的事情方面更加精确。向上和右动画只是一个测试,我的目标是能够向上,向下,向左或向右移动一个按钮,并且可能朝着相同的方向移动。例如,这是它的外观:

group->addAnimation(animationUp);
group->addAnimation(animationUp);
group->addAnimation(animationUp);
group->addAnimation(animationRight);
group->addAnimation(animationDown);
group->addAnimation(animationLeft);
group->addAnimation(animationLeft);
group->start();

在这种情况下,答案不再起作用。我尝试了2个设置,并遇到了不同的问题:

//Setting 1 : Only one of the Up animation is showed, the other is ignored
group->addAnimation(animationUp);
group->addAnimation(animationUp);
group->addAnimation(animationRight);

//Setting 2 : Up and Right working fine but the last Up starts from the starting position
group->addAnimation(animationUp);
group->addAnimation(animationRight);
group->addAnimation(animationUp);

问题是您要针对按钮的初始位置分配位置,必须创建一个QRect变量并使用translate()

对其进行修改
QPropertyAnimation* animationUp;
QPropertyAnimation* animationRight;
QSequentialAnimationGroup *group;
QRect r(ui->pushButton_2->pos(), QSize(buttonWidth, buttonHeight));
animationUp = new QPropertyAnimation(ui->pushButton_2, "geometry");
animationUp->setStartValue(r);
r.translate(0, -60);
animationUp->setEndValue(r);
animationRight = new QPropertyAnimation(ui->pushButton_2, "geometry");
animationRight->setStartValue(r);
r.translate(60, 0);
animationRight->setEndValue(r);
group = new QSequentialAnimationGroup;
group->addAnimation(animationUp);
group->addAnimation(animationRight);
group->start();

另外,如果您不想更改按钮的大小,则可以使用位置而不是几何形状。

ui->pushButton_2->resize(buttonWidth, buttonHeight);
QPropertyAnimation* animationUp;
QPropertyAnimation* animationRight;
QSequentialAnimationGroup *group;
QPoint p = ui->pushButton_2->pos();

animationUp = new QPropertyAnimation(ui->pushButton_2, "pos");
animationUp->setStartValue(p);
p += QPoint(0, -60);
animationUp->setEndValue(p);
animationRight = new QPropertyAnimation(ui->pushButton_2, "pos");
animationRight->setStartValue(p);
p += QPoint(60, 0);
animationRight->setEndValue(p);
group = new QSequentialAnimationGroup;
group->addAnimation(animationUp);
group->addAnimation(animationRight);
group->start();

Update :一种简单的方法是通过放置可能的动作并创建新动画来创建枚举,动画不参考先前的状态。

enum Movements{ Up, Down,Right, Left};
const auto dir = QList<QPoint>()<< QPoint(0, -1) << QPoint(0, 1)<<QPoint(1, 0)<<QPoint(-1, 0);

ui->pushButton_2->resize(buttonWidth, buttonHeight);
auto group = new QSequentialAnimationGroup;
QList<Movements> Sequence_of_movements;
Sequence_of_movements << Up << Up << Up << Right<<Down<<Left<<Left;
auto p = ui->pushButton_2->pos();
int step = 60;
for(auto i: Sequence_of_movements){
    auto animation = new QPropertyAnimation(ui->pushButton_2, "pos");
    animation->setStartValue(p);
    p += step*dir.at(i);
    animation->setEndValue(p);
    group->addAnimation(animation);
}
group->start();

相关内容

最新更新