Qtアニメーション☞QPropertyAnimation

概要概要

QPropertyAnimationクラスは、Qtのプロパティアニメーションを定義します。これは、主にプロパティを介してアニメーションを制御します。

QPropertyAnimationは、差分値としてQtプロパティを使用し、プロパティ値としてQVariantsに格納します。このクラスはQVariantAnimationを継承し、基本クラスと同じメタタイプのアニメーションをサポートします。

プロパティを宣言するクラスはQObjectである必要があります。プロパティをアニメーション効果として使用するには、セッターを指定する必要があります(このようにして、QPropertyAnimationはプロパティの値を設定できます)

実装手順

1.QPropertyAnimationオブジェクトを作成します。

2.アニメーションオブジェクトは、アニメーション化するオブジェクトにバインドされます(オブジェクトはQObjectから継承する必要があります)[setTargetObject]。

3.アニメーションのプロパティを実現するようにアニメーションオブジェクトを設定するには、プロパティはQ_PROPERTYマクロによって宣言され、プロパティの読み取り/書き込み関数[setPropertyName]を含める必要があります。設定できる属性は次のとおりです。

4.属性[setStartValueおよびsetEndValue]の開始値と終了値を設定します。

5.アニメーションの実行時間を設定します[setDuration]。

6.アニメーションを開始します[開始]。

ソースコード
 

#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");
}

 

おすすめ

転載: blog.csdn.net/weixin_41882459/article/details/113703514