当前位置:   article > 正文

Qt自定义动态切换QStackedWidget--QAnimationStackedWidget_qtstackedwidget 动画

qtstackedwidget 动画

一、效果展示

在这里插入图片描述


二、源码展示

#ifndef QANIMATIONSTACKEDWIDGET_H
#define QANIMATIONSTACKEDWIDGET_H

#include <QWidget>
#include <QList>
#include <QPropertyAnimation>

class QAnimationStackedWidget : public QWidget
{
    Q_OBJECT

public:
    QAnimationStackedWidget(QWidget *parent = nullptr);
    ~QAnimationStackedWidget();

    void setDuration(int duration);
    int addWidget(QWidget * widget);
    int insertWidget(int index, QWidget * widget);
    void removeWidget(QWidget * widget);
    void setCurrentWidget(QWidget * widget);
    void setCurrentIndex(int index);
    void resizeEvent(QResizeEvent *event);
    void moveAnimationStart();
    void setWidgetsVisible();

    int count() const;
    int currentIndex() const;
    int indexOf(QWidget * widget) const;
    QWidget* currentWidget() const;
    QWidget* widget(int index) const;

signals:
    void currentChanged(int);
    void widgetRemoved(int);

protected slots:
    void onValueChanged(const QVariant &value);

private:
    QList<QWidget* > m_widgetLst;

    int m_offset;
    int m_curIndex;
    int m_lastIndex;
    int m_duration;
    QPropertyAnimation* m_moveAnimation;


};
#endif // QANIMATIONSTACKEDWIDGET_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
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
#include "qanimationstackedwidget.h"

QAnimationStackedWidget::QAnimationStackedWidget(QWidget *parent)
    : QWidget(parent)
{
    m_offset = 0;
    m_curIndex = 0;
    m_lastIndex = 0;
    m_duration = 500;
    m_moveAnimation = new QPropertyAnimation(this, "");
    m_moveAnimation->setDuration(m_duration);
    connect(m_moveAnimation, &QPropertyAnimation::valueChanged, this, &QAnimationStackedWidget::onValueChanged);
}

QAnimationStackedWidget::~QAnimationStackedWidget()
{

}

void QAnimationStackedWidget::setDuration(int duration)
{
    m_duration = duration;
}

int QAnimationStackedWidget::addWidget(QWidget * widget)
{
    int index = indexOf(widget);
    if (index >= 0){
        return index;
    }
    widget->setParent(this);
    m_widgetLst.append(widget);
    return count() - 1;
}

int QAnimationStackedWidget::insertWidget(int index, QWidget * widget)
{
    int curindex = indexOf(widget);
    if (curindex >= 0) {
        return curindex;
    }
    widget->setParent(this);
    m_widgetLst.insert(index, widget);
    return index;
}

void QAnimationStackedWidget::removeWidget(QWidget * widget)
{
    int index = indexOf(widget);
    if (index >= 0) {
        m_widgetLst.removeAll(widget);
        emit widgetRemoved(index);
    }
}

void QAnimationStackedWidget::setCurrentWidget(QWidget * widget)
{
    int index = indexOf(widget);
    if (index >= 0 && m_curIndex != index) {
        setCurrentIndex(index);
    }
}

void QAnimationStackedWidget::setCurrentIndex(int index)
{
    if (index >= 0 && m_curIndex != index) {
        m_lastIndex = m_curIndex;
        m_curIndex = index;
        moveAnimationStart();
        emit currentChanged(index);
    }
}

void QAnimationStackedWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    int size = count();
    for (int i = 0; i < size; i++) {
        m_widgetLst.at(i)->resize(this->width(), this->height());
    }

    if (m_moveAnimation->state() == QAbstractAnimation::Running) {
        moveAnimationStart();
    }
    else {
        setWidgetsVisible();
    }
}

void QAnimationStackedWidget::onValueChanged(const QVariant &value)
{
    m_offset = value.toInt();
    m_widgetLst.at(m_curIndex)->move(m_offset, 0);
    if (m_curIndex > m_lastIndex) {
        m_widgetLst.at(m_lastIndex)->move(m_offset - this->width(), 0);
    }
    else {
        m_widgetLst.at(m_lastIndex)->move(this->width() + m_offset, 0);
    }
}

void QAnimationStackedWidget::moveAnimationStart()
{
    m_moveAnimation->stop();
    setWidgetsVisible();
    int startOffset = m_offset;
    if (m_curIndex > m_lastIndex) {
        if (startOffset == 0) startOffset = this->width();
        else startOffset = this->width() - qAbs(startOffset);
    }
    else {
        if (startOffset == 0) startOffset = -this->width();
        else startOffset = qAbs(startOffset) - this->width();
    }
    m_moveAnimation->setDuration(qAbs(startOffset) * m_duration / this->width());
    m_moveAnimation->setStartValue(startOffset);
    m_moveAnimation->setEndValue(0);
    m_moveAnimation->start();
}

void QAnimationStackedWidget::setWidgetsVisible()
{
    int size = count();
    for (int i = 0; i < size; i++) {
        if (m_lastIndex == i || m_curIndex == i)
            m_widgetLst.at(i)->setVisible(true);
        else {
            m_widgetLst.at(i)->setVisible(false);
        }
    }
}

int QAnimationStackedWidget::count() const
{
    return m_widgetLst.size();
}

int QAnimationStackedWidget::currentIndex() const
{
    return m_curIndex;
}

int QAnimationStackedWidget::indexOf(QWidget * widget) const
{
    return m_widgetLst.indexOf(widget);
}

QWidget * QAnimationStackedWidget::currentWidget() const
{
    if (m_curIndex >= 0 && m_curIndex < count()){
        return m_widgetLst.at(m_curIndex);
    }
    return nullptr;
}

QWidget * QAnimationStackedWidget::widget(int index) const
{
    if (index >= 0 && index < count()) {
        return m_widgetLst.at(index);
    }
    return nullptr;
}

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

闽ICP备14008679号