//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);
}