当前位置:   article > 正文

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

qparallelanimationgroup 页面平滑切换

简单粗暴,直接上代码:
#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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

#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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/81382
推荐阅读
相关标签
  

闽ICP备14008679号