qt实现抽屉效果,类似qq的好友功能

qt有自带的抽屉控件,叫qtoolbox,但是我觉得比较难用,于是便自己写了一个,基本的收缩啊,添加窗体功能都有的。直接看核心代码吧
.h

#ifndef MYDRAWER_H
#define MYDRAWER_H

#include <QWidget>
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QFrame>

class MyDrawer : public QFrame
{
    Q_OBJECT
public:
    explicit MyDrawer(QWidget *parent = nullptr);
    ~MyDrawer();

    void AddWidget(QWidget *Wgt);

    void SetTitle(QString i_title);

protected:
    void resizeEvent(QResizeEvent *event);
    void mousePressEvent(QMouseEvent *event);

signals:

private:
    QWidget *m_pCenterWgt=nullptr;
    QLabel *m_pTextLabel=nullptr;
    QLabel *m_pIconLabel=nullptr;
    QVBoxLayout *m_pCenterLayout=nullptr;

    QWidget *m_ptitleWgt=nullptr;

    QScrollArea *m_pArea=nullptr;

    bool m_ScaleBool=false;

};

#endif // MYDRAWER_H

.cpp

#include "mydrawer.h"
#include <QVBoxLayout>
#include <QResizeEvent>

MyDrawer::MyDrawer(QWidget *parent) : QFrame(parent)
{
    this->setFrameStyle(QFrame::Panel | QFrame::Raised);
    m_pCenterWgt=new QWidget(this);
    m_pCenterLayout=new QVBoxLayout(this);
    m_pCenterLayout->setMargin(5);
    m_pCenterLayout->setSpacing(5);
    m_pCenterWgt->setLayout(m_pCenterLayout);
    m_pTextLabel=new QLabel(this);
    m_pIconLabel=new QLabel(this);
    m_pIconLabel->setFixedSize(20,20);
    QPixmap pixMap(":/img/right.png");
    m_pIconLabel->setPixmap(pixMap);
    m_pTextLabel->setFixedWidth(150);

    m_ptitleWgt=new QWidget(this);
    QHBoxLayout *titleLayout=new QHBoxLayout(this);
    titleLayout->addWidget(m_pIconLabel);
    titleLayout->addWidget(m_pTextLabel);
    titleLayout->setMargin(0);
    titleLayout->setSpacing(3);
    m_ptitleWgt->setLayout(titleLayout);
    m_ptitleWgt->setFixedHeight(30);

    m_pArea=new QScrollArea(this);
    m_pArea->setWidget(m_pCenterWgt);

    m_pArea->hide();


    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(m_ptitleWgt);
    mainLayout->addWidget(m_pArea);
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    this->setLayout(mainLayout);
}

MyDrawer::~MyDrawer()
{

}

void MyDrawer::AddWidget(QWidget *Wgt)
{
    m_pCenterLayout->addWidget(Wgt);
}

void MyDrawer::SetTitle(QString i_title)
{
    m_pTextLabel->setText(i_title);
}

void MyDrawer::resizeEvent(QResizeEvent *event)
{
    m_pCenterWgt->resize(this->width(),this->height());
}

void MyDrawer::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        if(m_ptitleWgt->geometry().contains(this->mapFromGlobal(QCursor::pos())))
        {
            m_ScaleBool=!m_ScaleBool;
            if(m_ScaleBool)
            {
                m_pArea->show();
                m_pIconLabel->setPixmap(QPixmap(":/img/down.png"));
            }
            else
            {
                m_pArea->hide();
                m_pIconLabel->setPixmap(QPixmap(":/img/right.png"));
            }
        }
    }
}

然后看下效果图:
在这里插入图片描述

在这里插入图片描述
有什么不懂的欢迎在评论区留言。。。。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油吧,小杜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值