Qt 按钮不设置动画

Qt 按钮不设置动画,qt,animation,qtgui,Qt,Animation,Qtgui,我在Qt Designer中使用一个按钮创建了一个新的QWidget,并将其添加到源代码中: void MainWindow::startAnimation() { QPropertyAnimation animation(ui->pushButton, "geometry"); animation.setDuration(3000); animation.setStartValue(QRect(this->x(),this->y(),this->

我在Qt Designer中使用一个按钮创建了一个新的QWidget,并将其添加到源代码中:

void MainWindow::startAnimation()
{
    QPropertyAnimation animation(ui->pushButton, "geometry");
    animation.setDuration(3000);
    animation.setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation.setEndValue(QRect(this->x(),this->y(), 10,10));
    animation.setEasingCurve(QEasingCurve::OutBounce);
    animation.start();
}

void MainWindow::on_pushButton_clicked()
{
    startAnimation();
}

当我单击该按钮时,它将消失,并且不会产生动画。

您的
动画
超出范围,并在
startAnimation()函数的末尾自动删除。这就是为什么什么都没发生。使用
new
创建
QPropertyAnimation
实例,然后使用
finished
信号和
deleteLater
插槽将其删除,如下所示:

void MainWindow::startAnimation()
{
    QPropertyAnimation* animation = new QPropertyAnimation(ui->pushButton, "geometry");
    ...
    connect(animation, SIGNAL(finished()), animation, SLOT(deleteLater()));
    animation->start();
}

动画完成后,无需建立连接即可删除动画。只需在调用时将删除策略设置为QAbstractAnimation::DeleteWhenStopped<代码>动画->开始(QAbstractAnimation::DeleteWhenStopped)