Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QPropertyAnimation未保存小部件的最后位置_C++_Qt_Qt5 - Fatal编程技术网

C++ QPropertyAnimation未保存小部件的最后位置

C++ QPropertyAnimation未保存小部件的最后位置,c++,qt,qt5,C++,Qt,Qt5,我尝试使用多个QPropertyImation向上移动小部件,然后向右移动 代码如下: QPropertyAnimation* animationUp; QPropertyAnimation* animationRight; QSequentialAnimationGroup *group; animationUp = new QPropertyAnimation(this->ui->pushButton_2, "geometry"); animationUp->setSt

我尝试使用多个QPropertyImation向上移动小部件,然后向右移动

代码如下:

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();
在这种情况下,答案不再有效。我尝试了两种设置,但遇到了不同的问题:

//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()

此外,如果不打算更改按钮的大小,则可以使用位置而不是几何图形

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();
更新:一种简单的方法是通过放置可能的移动来创建枚举并创建新动画,动画不引用以前的状态

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();
enum移动{上、下、右、左};

const auto dir=QList()谢谢你的回答,它正在为向上和向右的动画工作,但我有更多的问题,我编辑了我的问题。你可以在移动类型中更明确。移动类型尚未定义,序列将由用户选择。有4种可能的运动,上、下、左、右,它们都是60个单位的平移。用户将能够从所有这些动作中进行选择,以创建其选择的序列。更新我的答案
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();