资源简介

QStackedWidget切换widget时的动画

资源截图

代码片段和文件信息

#include “animationstackedwidget.h“

#include 
#include 

AnimationStackedWidget::AnimationStackedWidget(QWidget *parent)
    : QStackedWidget(parent)
{
    m_isAnimating = false;
    m_currentValue = 0;
    m_currentIndex = 0;
    m_previousIndex = 0;

    m_animation = new QPropertyAnimation(this QByteArray());
    m_animation->setDuration(500);
    m_animation->setEasingCurve(QEasingCurve::Linear);
    m_animation->setStartValue(0);
    m_animation->setEndValue(0);
    connect(m_animation SIGNAL(valueChanged(QVariant)) SLOT(valueChanged(QVariant)));
    connect(m_animation SIGNAL(finished()) SLOT(animationfinished()));
}

AnimationStackedWidget::~AnimationStackedWidget()
{
    delete m_animation;
}

void AnimationStackedWidget::paintEvent(QPaintEvent * event)
{
    if(m_isAnimating)
    {
        QPainter painter(this);
        QTransform transform;
        renderCurrentWidget(painter transform);
        renderPreviousWidget(painter transform);
    }
    else
    {
        QWidget::paintEvent(event);
    }
}

void AnimationStackedWidget::renderPreviousWidget(QPainter &painter QTransform &transform)
{
    QWidget *w = widget(m_previousIndex);
    QPixmap pixmap( w->size() );
    w->render(&pixmap);

    Q_UNUSED(transform);
    switch(m_type)
    {
        case BottomToTop :
                {
                    painter.drawPixmap(0 height()/2 pixmap);
                    break;
                }
        case TopToBottom :
                {
                    painter.drawPixmap(0 -height()/2 pixmap);
                    break;
                }
        case LeftToRight :
                {
                    painter.drawPixmap(width()/2 0 pixmap);
                    break;
                }
        case RightToLeft :
                {
                    painter.drawPixmap(-width()/2 0 pixmap);
                    break;
                }
        default: break;
    }
}

void AnimationStackedWidget::renderCurrentWidget(QPainter &painter QTransform &transform)
{
    QWidget *w = widget(m_currentIndex);
    QPixmap pixmap( w->size() );
    w->render(&pixmap);

    switch(m_type)
    {
        case BottomToTop :
                {
                    transform.translate(0 m_currentValue);
                    painter.setTransform(transform);
                    painter.drawPixmap(0 -height()/2 pixmap);
                    break;
                }
        case TopToBottom :
                {
                    transform.translate(0 m_currentValue);
                    painter.setTransform(transform);
                    painter.drawPixmap(0 height()/2 pixmap);
                    break;
                }
        case LeftToRight :
                {
                    transform.translate(m_currentValue 0);
                    painter.setTransform(transform);
                    painter.drawPixmap(-width()/2 0 pixmap);
                    break;
                }
        case RightToLeft :
            

评论

共有 条评论