简体   繁体   English

QPropertyAnimation不动画

[英]QPropertyAnimation doesn't animate

This is my code: 这是我的代码:

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

I am newbie in C++, how can I make this work? 我是C ++的新手,如何进行这项工作?

I suggest you read a good C++ book, or at the very least go though http://www.cplusplus.com/doc/tutorial/ . 我建议您读一本不错的C ++书,或者至少阅读http://www.cplusplus.com/doc/tutorial/

For starters, you probably meant to call update() after a==1 in on_pushButton_clicked()? 对于初学者,您可能打算在on_pushButton_clicked()中a == 1之后调用update()? There is also a problem with your push button going out of scope at the end of the function, so you need to do 函数结尾处的按钮超出范围也存在问题,因此您需要执行

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

Finally, update() is a virtual function in QWidget (which I assume Widget derives?). 最后,update()是QWidget中的虚函数(我假设是Widget派生的?)。 Why are you overriding it? 为什么要覆盖它? You probably want to call it something like startAnimatinon() instead. 您可能想将其命名为类似startAnimatinon()的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM