上下收缩、折叠面板

效果:

        上下收缩、折叠面板,类似QQ好友列表那种。原理就是在一个布局中,通过button来实现一个独立widget的visible/disable

实现:

        1.分组按钮        

#ifndef EXPANDPANEL_H
#define EXPANDPANEL_H

#include <QWidget>

class QLabel;
class ExpandPanel : public QWidget
{
    Q_OBJECT
public:
    ExpandPanel(QWidget* parent=nullptr);
    void setTitle(const QString& title);

signals:
    void statuChanged(bool expand);

protected:
    void mousePressEvent(QMouseEvent *event);

private:
    QLabel* logo;
    QLabel* title;
    QLabel* icon;

    bool mExpand = true;
};

#endif // EXPANDPANEL_H











#include "expandpanel.h"
#include "parse/parsework.h"

#include <QDebug>
#include <QLabel>
#include <QVariant>
#include <QHBoxLayout>
#include <QStyle>
ExpandPanel::ExpandPanel(QWidget *parent)
    :QWidget(parent)
{            
    logo = new QLabel;
    logo->setObjectName("logo");
    title = new QLabel;
    title->setObjectName("title");
    icon = new QLabel;
    icon->setObjectName("icon");
    icon->setProperty("expand", true);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(logo);
    layout->addWidget(title);
    layout->addStretch(1);
    layout->addWidget(icon);
    layout->setContentsMargins(0,0,0,0);

    ParseWork::Instance().dynamicUpdateStyleSheet(this,
                ":/qss/src/qss/expandpanel.qss");
}

void ExpandPanel::setTitle(const QString &name)
{
    title->setText(name);
}

void ExpandPanel::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);

    mExpand = !mExpand;
    emit statuChanged(mExpand);
    icon->setProperty("expand", mExpand);
    ParseWork::Instance().dynamicUpdateStyleSheet(this,
                ":/qss/src/qss/expandpanel.qss");
}

2.使用

在mainwindow中创建两部分,左边是折叠面板,右侧是一个qtabwidget(用不到则删掉)

void MainWindow::createFrame()
{
    mFuncPanel = new QWidget;    
    mFuncVLayout = new QVBoxLayout;
    mFuncPanel->setMinimumWidth(220);
    mFuncPanel->setLayout(mFuncVLayout);
    mFuncVLayout->setSpacing(15);
    mFuncVLayout->setContentsMargins(0,0,0,0);

    mLogPanel = new QTabWidget;
    mLogPanel->setTabsClosable(true);
    mLogPanel->setElideMode(Qt::ElideLeft);

    QSplitter* spliter = new QSplitter;
    spliter->setOrientation(Qt::Horizontal);
    spliter->addWidget(mFuncPanel);
    spliter->addWidget(mLogPanel);
    spliter->setStretchFactor(1, 1);
    spliter->setChildrenCollapsible(false);//very importance, not to hide child duruing moving

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(spliter);
}


void MainWindow::initLocalFilePanel()
{
    mLocalFilePl = new ExpandPanel;
    mLocalFilePl->setTitle(tr("LocalFile"));

    LocalFile* file = new LocalFile;
    mFuncVLayout->addWidget(mLocalFilePl);
    mFuncVLayout->addWidget(file);
    mFuncVLayout->addStretch(1);

    connect(mLocalFilePl, &ExpandPanel::statuChanged, file, &LocalFile::setVisible);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值