查看完整版本: [-- Qt动效StackWidget --]

QTCN开发网 -> Qt 作品展 -> Qt动效StackWidget [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

305750665 2020-07-11 15:32

Qt动效StackWidget

## Qt动效StackWidget
@[toc]
### 功能
1. QStackWidget有的AStackWidget都有
2. 支持设置动效时常

### 效果图
[attachment=21732]


### 代码

```cpp
#include "AStackWidget.h"

#include <QDebug>
#include <QPropertyAnimation>

AStackWidget::AStackWidget(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, &AStackWidget::onValueChanged);
}

AStackWidget::~AStackWidget()
{

}

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

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

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

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

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

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

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

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

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

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

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

void AStackWidget::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 AStackWidget::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 AStackWidget::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 AStackWidget::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);
        }
    }
}
```

### Qt交流群
Qt交流大会 853086607

QQ:3246214072

305750665 2020-07-11 15:35
测试例子:

FrmAStackWidget::FrmAStackWidget(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    QList<QString> colorlst;
    colorlst << "#1abc9c";
    colorlst << "#2ecc71";
    colorlst << "#3498db";
    colorlst << "#9b59b6";
    colorlst << "#e74c3c";

    QList<QPushButton*> btnlst;
    btnlst << ui.pushButton_1;
    btnlst << ui.pushButton_2;
    btnlst << ui.pushButton_3;
    btnlst << ui.pushButton_4;
    btnlst << ui.pushButton_5;

    QButtonGroup *btnGroup = new QButtonGroup(this);
    connect(btnGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), ui.aStackwidget, &AStackWidget::setCurrentIndex);

    for (int i = 0; i < 5; i++) {
        QLabel *label = new QLabel(ui.aStackwidget);
        label->setStyleSheet(QString("background-color:%1;color:#ffffff;").arg(colorlst.at(i)));
        label->setText(QString::number(i + 1));
        label->setAlignment(Qt::AlignCenter);
        int index = ui.aStackwidget->addWidget(label);
        btnGroup->addButton(btnlst.at(i), index);
    }
}

boylebao 2020-07-11 15:51
  

alone_work 2020-07-26 09:44
    

oyp159753 2020-12-01 16:46
    

jobfind 2021-10-18 16:24
            

fanlab 2022-08-11 17:00


查看完整版本: [-- Qt动效StackWidget --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled