Qt开发动画

#include <QPropertyAnimation>

#include <QDesktopWidget>

//下坠

void MainWindow::on_pushButton_clicked()

{

QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "geometry");

QDesktopWidget *pDesktopWidget = QApplication::desktop();

int x = (pDesktopWidget->availableGeometry().width() - width()) / 2;

int y = (pDesktopWidget->availableGeometry().height() - height()) / 2;

pAnimation->setDuration(1000);

pAnimation->setStartValue(QRect(x, 0, width(), height()));

pAnimation->setEndValue(QRect(x, y, width(), height()));

pAnimation->setEasingCurve(QEasingCurve::OutElastic);

pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

}

//抖动

void MainWindow::on_pushButton_2_clicked()

{

QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos");

pAnimation->setDuration(500);

pAnimation->setLoopCount(2);

pAnimation->setKeyValueAt(0, QPoint(geometry().x() - 3, geometry().y() - 3));

pAnimation->setKeyValueAt(0.1, QPoint(geometry().x() + 6, geometry().y() + 6));

pAnimation->setKeyValueAt(0.2, QPoint(geometry().x() - 6, geometry().y() + 6));

pAnimation->setKeyValueAt(0.3, QPoint(geometry().x() + 6, geometry().y() - 6));

pAnimation->setKeyValueAt(0.4, QPoint(geometry().x() - 6, geometry().y() - 6));

pAnimation->setKeyValueAt(0.5, QPoint(geometry().x() + 6, geometry().y() + 6));

pAnimation->setKeyValueAt(0.6, QPoint(geometry().x() - 6, geometry().y() + 6));

pAnimation->setKeyValueAt(0.7, QPoint(geometry().x() + 6, geometry().y() - 6));

pAnimation->setKeyValueAt(0.8, QPoint(geometry().x() - 6, geometry().y() - 6));

pAnimation->setKeyValueAt(0.9, QPoint(geometry().x() + 6, geometry().y() + 6));

pAnimation->setKeyValueAt(1, QPoint(geometry().x() - 3, geometry().y() - 3));

pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

}

//透明度变化

void MainWindow::on_pushButton_3_clicked()

{

QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "windowOpacity");

pAnimation->setDuration(1000);

pAnimation->setKeyValueAt(0, 1);

pAnimation->setKeyValueAt(0.5, 0);

pAnimation->setKeyValueAt(1, 1);

pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

}

转自:http://blog.csdn.net/liang19890820/article/details/51888114

 

 

//逐渐出现

QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();

 

以上均为窗口动画。

 

 

控件动画:【均未成功,不知为何】

 

1、

QPropertyAnimation *pAnimation = new QPropertyAnimation(ui.groupBox, "opacity");
pAnimation->setDuration(1000);
pAnimation->setStartValue(0);
pAnimation->setEndValue(1);
pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

2、

QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect(ui.label);
effect->setOpacity(1);
ui.label->setGraphicsEffect(effect);
QPropertyAnimation *pAnimation = new QPropertyAnimation(effect, "opacity",this);
pAnimation->setEasingCurve(QEasingCurve::Linear);
pAnimation->setDuration(10000);
pAnimation->setStartValue(1);
pAnimation->setEndValue(0);
pAnimation->start(QAbstractAnimation::KeepWhenStopped);
delete effect;
delete pAnimation;

posted @ 2017-06-15 10:52  朱小勇  阅读(364)  评论(0编辑  收藏  举报