Qt封装QPropertyAnimation动画基类,所有界面通用实现界面类似手机滑动切换

简单粗暴,直接上代码:
#include “Cbaseanimation.h”

#ifndef CBASEANIMATION_H
#define CBASEANIMATION_H
#include <QObject>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QtWidgets/QWidget>
#include <QtWidgets/QLabel>
#include <QTimer>
// 界面动画基类
class CBaseAnimation : public QObject
{
    Q_OBJECT
public:
    CBaseAnimation();
    ~CBaseAnimation();

    void SetAnimation(QWidget *curwidget, QWidget *destwidget,bool directon);
//    void SetAnimation(QWidget *widget,int Duration,bool directon);

//    void SetTimeOutReturn(bool enable,uint mse = 0);


//    bool GetTimeOutReturnEnable();
private:
    QParallelAnimationGroup *group;
    QLabel * pixmapLabel;
    bool isAnimationFinish;
//    QWidgetList widgetlist;
//    QTimer * m_timer;
//    bool isTimeOutReturnEnable;
signals:
    void EM_AnimationFinish();
private slots:
//    void Slot_timeout();
public slots:
    void Slot_AnimationFinish();


};

extern CBaseAnimation * pBaseAnimation;

#endif // CBASEANIMATION_H

#include “Cbaseanimation.cpp”

#include "Cbaseanimation.h"
#include "DispManager.hpp"
#include <qDebug>
#include <QThread>
CBaseAnimation * pBaseAnimation = NULL;
CBaseAnimation::CBaseAnimation():
                isAnimationFinish(true)
{
    group = new QParallelAnimationGroup(this);
    connect(group,SIGNAL(finished()),this,SLOT(Slot_AnimationFinish()));
//    pixmapLabel = new QLabel;
//    m_timer = new QTimer(this);
//    connect(m_timer,SIGNAL(timeout()),this,SLOT(Slot_timeout()));
}

CBaseAnimation::~CBaseAnimation()
{
}

void CBaseAnimation::SetAnimation(QWidget *curwidget, QWidget *destwidget,bool directon)
{
//    QLabel *label = new QLabel();
//    label->resize(this->size());

//    QPixmap pic;
//    label->setPixmap(pic.grabWidget(this,this->rect()));
    label->setBuddy(this);
    label->setPixmap(*this->backgroundPixmap());
    label->setPalette(this->palette());
        label->setPixmap(this->backgroundRole();
//    label->show();


//    widgetlist.clear();
//    widgetlist.append(curwidget);
//    widgetlist.append(destwidget);

//    // 界面倒计时
//    if(timeoutReturnEnable)
//    {
//        m_timer->start(mse*1000);
//    }
//    else
//    {
//        m_timer->stop();
//    }


//    SetTimeOutReturn(timeoutReturnEnable,mse);//need
    if(!isAnimationFinish)
    {
        return;
    }

    destwidget->show();
    QPropertyAnimation *animation= new QPropertyAnimation(curwidget,"pos");
    animation->setDuration(300);
//    animation->setEasingCurve(QEasingCurve::OutBounce);
    QPropertyAnimation *animation1= new QPropertyAnimation(destwidget,"pos");
    animation1->setDuration(200);
//    animation1->setEasingCurve(QEasingCurve::OutBounce);
    if(directon)
    {
        animation->setStartValue(QPoint(0,0));//(0,0,curwidget->width(),curwidget->height()));
        animation->setEndValue(QPoint(-curwidget->width(),0));
        //(-curwidget->width(), 0, curwidget->width(), curwidget->height()));
        animation1->setStartValue(QPoint(destwidget->width()/3,0));//(destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
        animation1->setEndValue(QPoint(0,0));//(0,0,destwidget->width(), destwidget->height()));


//        animation->setStartValue(QRect(0,0,curwidget->width(),curwidget->height()));
//        animation->setEndValue(QRect(-curwidget->width(), 0, curwidget->width(), curwidget->height()));
//        animation1->setStartValue(QRect(destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
//        animation1->setEndValue(QRect(0,0,destwidget->width(), destwidget->height()));

    }
    else
    {

        animation->setStartValue(QPoint(0,0));//(0,0,curwidget->width(),curwidget->height()));
        animation->setEndValue(QPoint(curwidget->width(),0));//(-curwidget->width(), 0, curwidget->width(), curwidget->height()));
        animation1->setStartValue(QPoint(-destwidget->width()/3,0));//(destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
        animation1->setEndValue(QPoint(0,0));//(0,0,destwidget->width(), destwidget->height()));
//        animation->setStartValue(QRect(0,0,curwidget->width(),curwidget->height()));
//        animation->setEndValue(QRect(curwidget->width(), 0, curwidget->width(), curwidget->height()));
//        animation1->setStartValue(QRect(-destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
//        animation1->setEndValue(QRect(0,0,destwidget->width(), destwidget->height()));
    }

//    if(directon)
//    {
//        animation->setStartValue(QPoint(0,0));//(0,0,curwidget->width(),curwidget->height()));
//        animation->setEndValue(QPoint(-curwidget->width(),0));
//        //(-curwidget->width(), 0, curwidget->width(), curwidget->height()));
//        animation1->setStartValue(QPoint(destwidget->width()/2,0));//(destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
//        animation1->setEndValue(QPoint(0,0));//(0,0,destwidget->width(), destwidget->height()));


        animation->setStartValue(QRect(0,0,curwidget->width(),curwidget->height()));
        animation->setEndValue(QRect(-curwidget->width(), 0, curwidget->width(), curwidget->height()));
        animation1->setStartValue(QRect(destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
        animation1->setEndValue(QRect(0,0,destwidget->width(), destwidget->height()));

//    }
//    else
//    {

//        animation->setStartValue(QPoint(0,0));//(0,0,curwidget->width(),curwidget->height()));
//        animation->setEndValue(QPoint(curwidget->width(),0));//(-curwidget->width(), 0, curwidget->width(), curwidget->height()));
//        animation1->setStartValue(QPoint(-destwidget->width()/2,0));//(destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
//        animation1->setEndValue(QPoint(0,0));//(0,0,destwidget->width(), destwidget->height()));
        animation->setStartValue(QRect(0,0,curwidget->width(),curwidget->height()));
        animation->setEndValue(QRect(curwidget->width(), 0, curwidget->width(), curwidget->height()));
        animation1->setStartValue(QRect(-destwidget->width()/2, 0, destwidget->width(), destwidget->height()));
        animation1->setEndValue(QRect(0,0,destwidget->width(), destwidget->height()));
//    }


//    QPropertyAnimation *animation2 = new QPropertyAnimation(destwidget,"windowOpacity");
//    animation2->setTargetObject(destwidget);
//    animation2->setDuration(Duration);
//    animation2->setStartValue(0);
//    animation2->setEndValue(1);

    group->clear();
    group->addAnimation(animation);
    group->addAnimation(animation1);
//    group->addAnimation(animation2);
    group->start();
    isAnimationFinish = false;


}

//void CBaseAnimation::SetAnimation(QWidget *widget,int Duration,bool directon)
//{
    QLabel *label = new QLabel();
    label->resize(this->size());

    QPixmap pic;
    label->setPixmap(pic.grabWidget(this,this->rect()));
//    label->setBuddy(this);
//    label->setPixmap(*this->backgroundPixmap());
//    label->setPalette(this->palette());
//        label->setPixmap(this->backgroundRole();
    label->show();

//    widget->move(0,0);

    pixmapLabel->setWindowFlags(Qt::FramelessWindowHint | Qt::W1indowStaysOnTopHint| Qt::Tool);//

//    pixmapLabel->setFixedSize(widget->width(),widget->height());
//    pixmapLabel->show();

//    QPropertyAnimation *animation= new QPropertyAnimation(pixmapLabel,"geometry");
//    animation->setDuration(600);
//    QPropertyAnimation *animation1= new QPropertyAnimation(widget,"geometry");
//    animation1->setDuration(400);

//    if(directon)
//    {
//        animation->setStartValue(QRect(0,0,pixmapLabel->width(), pixmapLabel->height()));
//        animation->setEndValue(QRect(-pixmapLabel->width(), 0, pixmapLabel->width(), pixmapLabel->height()));
//        animation1->setStartValue(QRect(widget->width()/2, 0, widget->width(), widget->height()));
//        animation1->setEndValue(QRect(0,0,widget->width(), widget->height()));
//    }
//    else
//    {
//        animation->setStartValue(QRect(0,0,pixmapLabel->width(), pixmapLabel->height()));
//        animation->setEndValue(QRect(pixmapLabel->width(), 0, pixmapLabel->width(), pixmapLabel->height()));
//        animation1->setStartValue(QRect(-widget->width(), 0, widget->width(), widget->height()));
//        animation1->setEndValue(QRect(0,0,widget->width(), widget->height()));
//    }

//    QParallelAnimationGroup *group = new QParallelAnimationGroup;
//    group->addAnimation(animation1);
//    group->addAnimation(animation);
//    group->start(QParallelAnimationGroup::DeleteWhenStopped);

//    connect(group,SIGNAL(finished()),this,SLOT(Slot_AnimationFinish()));
//}


void CBaseAnimation::Slot_AnimationFinish()
{
//    widgetlist.at(0)->hide();
    isAnimationFinish = true;
    emit EM_AnimationFinish();
//    pixmapLabel->hide();
}

//void CBaseAnimation::Slot_timeout()
//{
//    m_timer->stop();
//    if(widgetlist.count() > 0)
//    {
//        SetAnimation(widgetlist.at(1),CDispManager::pUIDisplay,400,false,false);
//    }
//}

//void CBaseAnimation::SetTimeOutReturn(bool enable,uint mse)
//{
//    isTimeOutReturnEnable = enable;
//    if(isTimeOutReturnEnable)
//    {
//       m_timer->start(mse*1000);

//    }
//    else
//    {
//       m_timer->stop();
//    }

//}

//bool CBaseAnimation::GetTimeOutReturnEnable()
//{
//    return this->isTimeOutReturnEnable;
//}

里面很多注释的可以忽略或者增加扩展也是可以的~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值