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


    • 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 我为大家推荐一个我写的项目
  • 相关阅读:
    代码操作
    购物车
    利息计算器
    生成海报
    知识库
    JavaScript处理字符串--参照W3C
    C#输入排序-冒泡
    enum举例
    C# 表达式计算器----数据结构
    C# 测试单词的完美度
  • 原文地址:https://www.cnblogs.com/senior-engineer/p/8125300.html
Copyright © 2020-2023  润新知