QPropertyAnimation没有动画效果

时间:2012-05-25 17:50:22

标签: c++ qt animation

这是我的代码:

void Widget::update()
{
    if (a==1)
    {
        QPushButton button("Animated Button");
        button.show();

        QPropertyAnimation *animation =
                    new QPropertyAnimation(&button, "geometry");
        animation->setDuration(10000);
        animation->setStartValue(QRect(0, 0, 100, 30));
        animation->setEndValue(QRect(250, 250, 100, 30));

        animation->start();
        a++;
    }
}

void Widget::on_pushButton_clicked()
{
    a=1;
}

我是C ++的新手,我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我建议你阅读一本好的C ++书,或者至少去看http://www.cplusplus.com/doc/tutorial/

对于初学者,您可能打算在on_pushButton_clicked()中的== 1之后调用update()?在功能结束时,按钮超出范围也存在问题,因此您需要执行

QPushButton *button = new QPushButton("Animated Button", this); 

最后,update()是QWidget中的虚函数(我假设Widget派生?)。你为什么要压倒它?你可能想把它叫做startAnimatinon()而不是。

相关问题