Qt实现侧边栏(右边)渐入渐出,类似抽屉

//sidewidget.h

 

#ifndef SIDEWIDGET_H
#define SIDEWIDGET_H
 
#include <QWidget>
#include <QTimer>
 
namespace Ui {
class SideWidget;
}
namespace Dcom
{
    enum SpeedType
    {
        SLOW = 1,FAST = 2
    };
}
 
 
class SideWidget : public QWidget
{
    Q_OBJECT
 
public:
    explicit SideWidget(QWidget *parent = nullptr);
    ~SideWidget();
    void setCenterWidget(QWidget *);
    void setSpeed(Dcom::SpeedType = Dcom::SLOW);
private:
    Ui::SideWidget *ui;
    //中心区域
    QWidget *m_pCenterWidget;
    //是否是首次打开,默认不展开
    int m_first = 0;
    //定时器
    QTimer *m_pTimer;
    //总共步数,默认为m_pCenterWidget的宽度像素
    int m_steps = 0;
    //父类的宽度
    int m_parentWidth = 0;
    //是否展开
    bool m_isExpend = false;
    //速度
    int m_speed = 1;
public slots:
    //窗口重置
    void resize_action(int,int);
private slots:
    void on_btnExpend_clicked();
    void expendsAction();
};
 
#endif // SIDEWIDGET_H
//sidewidget.cpp
#include "sidewidget.h"
#include "ui_sidewidget.h"
#include <QDebug>
#include <QDesktopWidget>
 
SideWidget::SideWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SideWidget)
{
    ui->setupUi(this);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,255));
    setPalette(pal);
    m_pTimer = new QTimer(this);
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(expendsAction()));
    connect(parent,SIGNAL(resizeWindow(int,int)),this,SLOT(resize_action(int,int)));
}
 
void SideWidget::setSpeed(Dcom::SpeedType type)
{
    this->m_speed = type;
}
 
void SideWidget::setCenterWidget(QWidget *centerWidget)
{
    centerWidget->show();
    ui->centerGrid->addWidget(centerWidget);
    m_pCenterWidget = centerWidget;
    this->setMinimumSize(m_pCenterWidget->frameGeometry().width()+ui->btnExpend->width(),m_pCenterWidget->frameGeometry().height());
    this->setMaximumSize(m_pCenterWidget->frameGeometry().width()+ui->btnExpend->width(),m_pCenterWidget->frameGeometry().height());
    this->show();
}
void SideWidget::resize_action(int width,int)
{
    m_parentWidth = width;
    if(m_first == 0)
    {
        this->setGeometry(width-ui->btnExpend->width(),0,m_pCenterWidget->frameGeometry().width(),m_pCenterWidget->frameGeometry().height());
        m_first = 1;
    }
    else
    {
        if(m_isExpend)
        {
            this->setGeometry(width-m_pCenterWidget->frameGeometry().width()-ui->btnExpend->width(),0,m_pCenterWidget->frameGeometry().width(),m_pCenterWidget->frameGeometry().height());
        }
        else
        {
            this->setGeometry(width-ui->btnExpend->width(),0,m_pCenterWidget->frameGeometry().width(),m_pCenterWidget->frameGeometry().height());
        }
 
    }
    update();
}
 
SideWidget::~SideWidget()
{
    delete ui;
}
 
void SideWidget::expendsAction()
{
    if(!m_isExpend)
    {
        if(m_steps<=m_pCenterWidget->frameGeometry().width()-ui->btnExpend->width())
        {
            //展开
             m_steps+=m_speed;
            this->setGeometry(m_parentWidth-m_pCenterWidget->frameGeometry().width()+m_steps,0,m_pCenterWidget->frameGeometry().width(),m_pCenterWidget->frameGeometry().height());
 
        }
        else
        {
            m_pTimer->stop();
        }
    }
    else
    {
        if(m_steps<=m_pCenterWidget->frameGeometry().width()+ui->btnExpend->width())
        {
            m_steps+=m_speed;
            this->setGeometry(m_parentWidth-m_steps,0,m_pCenterWidget->frameGeometry().width(),m_pCenterWidget->frameGeometry().height());
        }
        else
        {
            m_pTimer->stop();
        }
 
    }
}
void SideWidget::on_btnExpend_clicked()
{
    m_steps = 0;
    m_isExpend = !m_isExpend;
    if(ui->btnExpend->text()=="open")
    {
        ui->btnExpend->setText(QString::fromLocal8Bit("close"));
    }
    else
    {
        ui->btnExpend->setText(QString::fromLocal8Bit("open"));
    }
    m_pTimer->start(1);
}

https://download.csdn.net/download/weixin_43419112/12698312 

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值