widget.h
#include "widget.h"
#include "ui_widget.h"
#include <QGraphicsColorizeEffect>
#include <QGraphicsOpacityEffect>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
propertyAnimation = new QPropertyAnimation(ui->geometryWidget,"geometry");
//设置动画起始值、关键值、终止值;
propertyAnimation->setStartValue(QRect(0,0,100,100));
propertyAnimation->setKeyValueAt(0.5,QRect(0,400,200,200));
propertyAnimation->setEndValue(QRect(600,400,200,200));
//设置动画时常;
propertyAnimation->setDuration(2000);
//设置动画循环周期;
propertyAnimation->setLoopCount(1);
//设置动画缓和曲线;动画运动速度曲线;
propertyAnimation->setEasingCurve(QEasingCurve::InOutCubic);
//设置颜色动画;
QGraphicsColorizeEffect *graphicsColorizeEffect = new QGraphicsColorizeEffect(this);
ui->colorWidget->setGraphicsEffect(graphicsColorizeEffect);
propertyAnimation1 = new QPropertyAnimation(graphicsColorizeEffect,"color");
propertyAnimation1->setStartValue(QColor(Qt::black));
propertyAnimation1->setEndValue(QColor(Qt::red));
propertyAnimation1->setDuration(4000);
//不透明度动画
QGraphicsOpacityEffect *graphicsOpacityEffect = new QGraphicsOpacityEffect(this);
ui->colorWidget->setGraphicsEffect(graphicsOpacityEffect);
propertyAnimation2 = new QPropertyAnimation(graphicsOpacityEffect,"opacity");
propertyAnimation2->setStartValue(0.0);
propertyAnimation2->setEndValue(1.0);
propertyAnimation2->setDuration(2000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
propertyAnimation->start();
propertyAnimation2->start();
}
void Widget::on_pushButton_2_clicked()
{
propertyAnimation1->start();
}