• 680阅读
  • 3回复

Qt动效StackWidget [复制链接]

上一主题 下一主题
离线305750665
 

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

### 效果图



### 代码

```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
雨田哥: 群号:853086607(收费群)
专业定制桌面应用软件(QQ: 3246214072)

刘典武-feiyangqingyun:专业各种自定义控件编写+UI定制+输入法定制+视频监控+工业控制+仪器仪表+嵌入式linux+各种串口网络通信,童叟无欺,量大从优,欢迎咨询购买定制!QQ:517216493

QtClub: http://cms.heilqt.com/   教主:寒山-居士
离线305750665

只看该作者 1楼 发表于: 07-11
测试例子:

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);
    }
}
雨田哥: 群号:853086607(收费群)
专业定制桌面应用软件(QQ: 3246214072)

刘典武-feiyangqingyun:专业各种自定义控件编写+UI定制+输入法定制+视频监控+工业控制+仪器仪表+嵌入式linux+各种串口网络通信,童叟无欺,量大从优,欢迎咨询购买定制!QQ:517216493

QtClub: http://cms.heilqt.com/   教主:寒山-居士
在线boylebao

只看该作者 2楼 发表于: 07-11
  
为加快Qt开发而造轮子。
离线alone_work

只看该作者 3楼 发表于: 07-26
    
快速回复
限100 字节
 
上一个 下一个