In this example, we will learn how to animate our Graphical User Interface (GUI) elements using Qt's property animation class, part of its powerful animation framework, which allows us to create fluid looking animation with minimal effort.
First, let's create a new Qt Widgets Application project. After that, open up
mainwindow.uiwith Qt Designer and place a button on the main window, as shown here:
Next, open up
mainwindow.cppand add the following line of code at the beginning of the source code:
QPropertyAnimation *animation = new QPropertyAnimation(ui->pushButton, "geometry"); animation->setDuration(10000); animation->setStartValue(ui->pushButton->geometry()); animation->setEndValue(QRect(200, 200, 100, 50)); animation->start();