(QT)属性动画

目录

一、QPropertyAnimation

1、概念

2、使用示例

二、Q_PROPERTY自定义属性动画

1、概念

2、使用


一、QPropertyAnimation

1、概念

①QPropertyAnimation是Qt框架中的一个类,用于在给定时间间隔内对QObject的属性进行动画化的平滑过渡。可以使用QPropertyAnimation实现各种动画效果,如淡入淡出、位移、旋转等。

②QPropertyAnimation使用setStartValue()和setEndValue()方法指定动画的起始值和结束值,使用setDuration()方法指定动画持续的时间,使用setEasingCurve()方法指定动画的缓动曲线。调用start()方法开始动画。

③QPropertyAnimation也可以使用setValue()方法手动设置动画的当前值。此外,QPropertyAnimation还提供了一些信号,如valueChanged()和finished(),可用于处理动画过程中的事件。


2、使用示例

#include "widget.h"
#include "ui_widget.h"
#include 
#include 

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    /**********××××××××××××××× 几何动画 ××××××××××××××************/
    //几何属性动画初始化                              目标          属性
    propertyAnimation = new QPropertyAnimation(ui->geometry,"geometry");
    //设置动画起始值 QRect(int left, int top, int width, int height)
    propertyAnimation->setStartValue(QRect(0,0,100,100));
    //设置某个时间段值
    propertyAnimation->setKeyValueAt(0.25,QRect(600,0,100,100));
    propertyAnimation->setKeyValueAt(0.50,QRect(600,400,200,200));
    propertyAnimation->setKeyValueAt(0.75,QRect(0,400,300,300));
    propertyAnimation->setEndValue(QRect(0,0,400,400));//设置结束值
    propertyAnimation->setDuration(5000);//设置动画时长
    propertyAnimation->setLoopCount(1);//设置动画循环周期
    propertyAnimation->setEasingCurve(QEasingCurve::InOutQuad);//设置动画的缓和曲线(动画曲线)


    /**********××××××××××××××××× 颜色动画 ×××××××××××××××************/
    QGraphicsColorizeEffect *graphicsColorizeEffect=new QGraphicsColorizeEffect(this);
    ui->color->setGraphicsEffect(graphicsColorizeEffect);
    propertyAnimation1 = new QPropertyAnimation(graphicsColorizeEffect,"color");
    propertyAnimation1->setStartValue(QColor(Qt::blue));
    propertyAnimation1->setKeyValueAt(0.50,QColor(Qt::green));
    propertyAnimation1->setEndValue(QColor(Qt::red));
    propertyAnimation1->setDuration(3000);


    /**********××××××××××××××××× 不透明度动画 ×××××××××××××××************/
    QGraphicsOpacityEffect *graphicsOpacityEffect=new QGraphicsOpacityEffect(this);
    ui->opacity->setGraphicsEffect(graphicsOpacityEffect);
    propertyAnimation2 = new QPropertyAnimation(graphicsOpacityEffect,"opacity");
    propertyAnimation2->setStartValue(1.0);
    propertyAnimation2->setKeyValueAt(0.5,0.0);//setKeyValueAt(关键帧,透明度)
    propertyAnimation2->setEndValue(1.0);
    propertyAnimation2->setDuration(3000);
}

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

void Widget::on_pushButton_clicked()
{
    propertyAnimation->start();
}

void Widget::on_pushButton_2_clicked()
{
    propertyAnimation1->start();
}

void Widget::on_pushButton_3_clicked()
{
    propertyAnimation2->start();
}

二、Q_PROPERTY自定义属性动画

1、概念

Q_PROPERTY是Qt框架中的一个宏,用于将C++类中的成员变量(即属性)声明为Qt属性,以便在Qt框架中使用。使用Q_PROPERTY宏,可以在C++类中定义一个属性,然后将其与Qt的元对象系统(Meta-Object System)相关联。这使得该属性可以通过Qt框架中的信号和槽机制进行访问和修改,还可以使用Qt的属性系统进行序列化和反序列化。

2、使用

2.1 自定义类

rich.h

#ifndef RICH_H
#define RICH_H

#include 

namespace Ui {
class Rich;
}

class Rich : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(qreal money READ money WRITE setMoney)

public:
    explicit Rich(QWidget *parent = nullptr);
    ~Rich();
    qreal money() const;
    void setMoney(qreal m);
private:
    Ui::Rich *ui;
    qreal richMoney;
};

#endif // RICH_H

rich.c

#include "rich.h"
#include "ui_rich.h"

Rich::Rich(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Rich)
    //richMoney(500)
{
    ui->setupUi(this);
}

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

qreal Rich::money() const
{
    return richMoney;
}

void Rich::setMoney(qreal m)
{
    //richMoney+=m;
    richMoney=m;
    ui->label->setText(QString::number(richMoney)+"W");
}

2.2 widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 
#include 

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    rich=new Rich(this);
    //rich->setProperty("money",200);//会自动调用WRITE方法
    qDebug()<money()<setStartValue(100);
    propertyAnimation->setEndValue (600);
    propertyAnimation->setDuration(10000);
    propertyAnimation->setEasingCurve(QEasingCurve::Linear);
    propertyAnimation->start();

}

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

你可能感兴趣的:(QT学习笔记,qt,动画,开发语言)