QPropertyAnimation 几行代码快速制作流畅的动画效果

原文地址::http://blog.csdn.net/csnd_ayo/article/details/53790217

相关文章

1、Qt 动画类 - QPropertyAnimation Class (翻译)----http://blog.csdn.net/csnd_ayo/article/details/53770611

2、Qt简介----http://blog.csdn.net/csnd_ayo/article/details/70170882


  • QPropertyAnimation Class 官方英文文档【点击前往
  • QPropertyAnimation Class 中文译文文档【点击前往

简介

QPropertyAnimation Class 是一个控制动画效果的类,诞生自 Qt 4.6 版本。 该类继承自 QVarianAnimation,并支持其它基类相同的动画类,例如:QAnimationGroup 动画组类,该类仅支持继承自 QObject 类的窗口部件。

以例代劳

用例子来讲述各个功能,直观,立竿见影。

头文件

[cpp]  view plain  copy
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3. #include <QMainWindow>  
  4. namespace Ui {  
  5. class MainWindow;  
  6. }  
  7.   
  8. class MainWindow : public QMainWindow  
  9. {  
  10.     Q_OBJECT  
  11. public:  
  12.     explicit MainWindow(QWidget *parent = 0);  
  13.     ~MainWindow();  
  14. private:  
  15.     Ui::MainWindow *ui;  
  16. };  
  17. #endif // MAINWINDOW_H  


cpp文件

[cpp]  view plain  copy
  1. #include <QPropertyAnimation>  
  2. #include "mainwindow.h"  
  3. #include "ui_mainwindow.h"  
  4.   
  5. MainWindow::MainWindow(QWidget *parent) :  
  6.     QMainWindow(parent),  
  7.     ui(new Ui::MainWindow)  
  8. {  
  9.     ui->setupUi(this);  
  10.   
  11.     /*  声明动画类,并将控制对象 this (this一定是继承自QObject的窗口部件)  以及属性名 "geometry" 传入构造函数  */  
  12.     QPropertyAnimation* animation = new QPropertyAnimation(this"geometry");  
  13.     /*  设置动画持续时长为 2 秒钟  */  
  14.     animation->setDuration(2000);  
  15.     /*  设置动画的起始状态 起始点 (1,2)  起始大小 (3,4)  */  
  16.     animation->setStartValue(QRect(1, 2, 3, 4));  
  17.     /*  设置动画的结束状态 结束点 (100,200)  结束大小 (300,400)  */  
  18.     animation->setEndValue(QRect(100, 200, 300, 400));  
  19.     /*  设置动画效果  */  
  20.     animation->setEasingCurve(QEasingCurve::OutInExpo);  
  21.     /*  开始执行动画 QAbstractAnimation::DeleteWhenStopped 动画结束后进行自清理(效果就好像智能指针里的自动delete animation) */  
  22.     animation->start(QAbstractAnimation::DeleteWhenStopped);  
  23. }  
  24.   
  25. MainWindow::~MainWindow()  
  26. {  
  27.     delete ui;  
  28. }  



QPropertyAnimation 声明的时候
可以传入的属性分别有  pos(位置)、windowOpacity(透明度)

位置示例

[cpp]  view plain  copy
  1. void OEasyWebNotice::onShow() {  
  2.     QRect rect = QApplication::desktop()->availableGeometry();  
  3.     const int &endy = rect.height() - height();  
  4.     QPropertyAnimation *animation= new QPropertyAnimation(this,"pos");  
  5.     animation->setDuration(2000);  
  6.     animation->setStartValue(QPoint(rect.width() - width(), rect.height()));  
  7.     animation->setEndValue(QPoint(rect.width() - width(), endy));  
  8.     animation->setEasingCurve(QEasingCurve::OutCubic);  
  9.   
  10.     connect(animation, SIGNAL(finished()),  
  11.             this, SLOT(animationFinished()));  
  12.     show();  
  13.     animation->start(QAbstractAnimation::DeleteWhenStopped);  
  14. }  



透明度示例

[cpp]  view plain  copy
  1. void OEasyWebNotice::onClose(void) {  
  2.     disconnect(closeButton_.get(),SIGNAL(clicked()),  
  3.             this, SLOT(onClose()));  
  4.   
  5.     QPropertyAnimation* animation = new QPropertyAnimation(this"windowOpacity");  
  6.     animation->setDuration(1000);  
  7.     animation->setStartValue(1);  
  8.     animation->setEndValue(0);  
  9.     animation->setEasingCurve(QEasingCurve::InCirc);  
  10.     connect(animation, SIGNAL(finished()),  
  11.             this, SLOT(deleteLater()));  
  12.     show();  
  13.     animation->start(QAbstractAnimation::DeleteWhenStopped);  
  14. }  


关于 QPropertyAnimation 我为大家推荐一个我写的项目


版权声明:分享得以延续,交流方知彼此。作者:_OE_ 博客地址:http://blog.csdn.net/csnd_ayo



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值