Qt animation ☞QPropertyAnimation

Overview

The QPropertyAnimation class defines Qt's property animation, which mainly controls the animation through properties.

QPropertyAnimation uses Qt properties as the difference value and stores them in QVariants as the property value. This class inherits from QVariantAnimation and supports the same meta-type animation of the base class.

The class that declares the property must be a QObject. In order for the property to be used as an animation effect, a setter must be provided (in this way, QPropertyAnimation can set the value of the property)

Implementation steps

1. Create a QPropertyAnimation object.

2. The animation object is bound to the object to be animated (the object must be inherited from QObject) [setTargetObject].

3. To set the animation object to realize the properties of the animation, the properties are declared by the Q_PROPERTY macro and must include the read-write function [setPropertyName] of the properties. The attributes that can be set are as follows:

4. Set the start value and end value of the attribute [setStartValue and setEndValue].

5. Set the animation running time [setDuration].

6. Start the animation [start].

Source code
 

#pragma execution_character_set("utf-8")
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->label->setPixmap(QPixmap(":/image/cut.PNG"));
    animation = new QPropertyAnimation;
    animation->setTargetObject(ui->label);
    animation->setPropertyName("size");
    animation->setStartValue(QSize(0,0));
    animation->setEndValue(QSize(300,250));
    animation->setDuration(4000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_startBtn_clicked()
{
    if(ui->startBtn->text()=="start")
    {
        animation->start();
        ui->startBtn->setText("pause");
    }
    else if(ui->startBtn->text()=="pause")
    {
        animation->pause();
        ui->startBtn->setText("continue");
    }
    else if(ui->startBtn->text()=="continue")
    {
        animation->resume();
        ui->startBtn->setText("pause");
    }
}

void MainWindow::on_stopBtn_clicked()
{
    animation->stop();
    ui->startBtn->setText("start");
}

 

Guess you like

Origin blog.csdn.net/weixin_41882459/article/details/113703514