Basic animation on QPushbutton



  • Hi,

    Im trying to do a basic animation on a clicked qpushbutton to reduce it but nothing happends.

    I have only a a QPushButton design with Qt designer on the MainWindow. Heres the code of the MainWindow :

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QPropertyAnimation>
    #include <QStateMachine>
    #include <QSignalTransition>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(test()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;

    }

    #include <iostream>

    void MainWindow::test() {
    std::cout << "enter in function : " << std::endl;
    QPropertyAnimation animation(ui->pushButton, "geometry");
    animation.setDuration(10000);
    animation.setStartValue(QRect(ui->pushButton->x(), ui->pushButton->y(),
    ui->pushButton->width(), ui->pushButton->height()));
    animation.setEndValue(QRect(0,0,0,0));

    animation.setEasingCurve(QEasingCurve::OutBounce);
    
    animation.start();
    

    }

    I have tried to put the QPushButton in a Layout but it does the same.

    Does anyone have an idea ?

    Thank you.

    EDIT //

    I set the animation in the mainwindow class and it worked !


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you allocate your animation on the stack it's destroyed at the end of test, so just after the call to start so it doesn't have the time to run.



  • Thanks a lot it works now, but i have a question, how can I reduce a QPushButton without a starting point ? i want the center of the qpushbutton to be the same when resized



  • I could not find any setOrigin method in QPropertyAnimation, so maybe a solution would be to combine two animations: yours and a translation that centres the button at the same speed. You can look at http://doc.qt.io/qt-5/qparallelanimationgroup.html for parallel animations.



  • Yeah the only problem is that i dont get how to make a transition from a position to another


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.