1、简介
最近要做一个项目,需要用到抽屉,但是抽屉有不能同时展开多个,所以自己用Layout实现下,记录在此,备忘。 主要思路是自定义一个类继承自QWidget,此类包含一个QVBoxLayout作为mainlayout,然后在mainlayout中顶部用一个按钮控件来作为抽屉的标题栏,用一个list保存addItem插入的控件,当点击顶部按钮时,把list保存的控件可视化状态改变,下面贴具体代码。2、代码
MyToolBox.h:#pragma once
#include <QWidget>
#include <QVBoxLayout>
#include <QList>
#include <QPushButton>
class MyToolBox :
public QWidget
{
Q_OBJECT
public:
MyToolBox(QIcon & icon, const QString &title, QWidget *parent = 0);
~MyToolBox(void);
void addItem(QWidget *item);
private slots:
void titleClicked();
private:
QVBoxLayout *m_mainLayout;
QList<QWidget *> m_itemList;
QPushButton *m_TitleBtn;
bool m_isVisable;
};
MyToolBox.cpp:
#include "MyToolBox.h"customtoolbox.h:
MyToolBox::MyToolBox(QIcon & icon, const QString &title, QWidget *parent)
: QWidget(parent)
{
//初始化为非展开
m_isVisable = false;
m_mainLayout = new QVBoxLayout;
m_TitleBtn = new QPushButton(icon, title);
m_mainLayout->addWidget(m_TitleBtn);
m_mainLayout->addSpacing(2);
connect(m_TitleBtn, SIGNAL(clicked()), this, SLOT(titleClicked()));
m_mainLayout->setContentsMargins(0,0,0,0);
m_mainLayout->setSpacing(0);
setLayout(m_mainLayout);
setStyleSheet("QWidget{border: 0;}");
}
MyToolBox::~MyToolBox(void)
{
}
void MyToolBox::titleClicked()
{
for (int i=0; i<m_itemList.size(); ++i)
{
m_itemList[i]->setVisible(!m_itemList[i]->isVisible());
}
if (m_itemList.size() > 0)
m_isVisable = m_itemList[0]->isVisible();
}
void MyToolBox::addItem(QWidget *item)
{
m_itemList.append(item);
m_mainLayout->addWidget(item);
if (0 == m_itemList.size())
item->setVisible(false);
else
{
item->setVisible(m_isVisable);//m_itemList[0]->isVisible());
}
}
#ifndef CUSTOMTOOLBOX_Hcustomtoolbox.cpp:
#define CUSTOMTOOLBOX_H
#include <QtGui/QWidget>
#include <QVBoxLayout>
#include "MyToolBox.h"
class CustomToolBox : public QWidget
{
Q_OBJECT
public:
CustomToolBox(QWidget *parent = 0, Qt::WFlags flags = 0);
~CustomToolBox();
private:
MyToolBox * m_toolBox1;
MyToolBox * m_toolBox2;
MyToolBox * m_toolBox3;
QVBoxLayout *m_mainLayout;
};
#endif // CUSTOMTOOLBOX_H
#include "customtoolbox.h"
CustomToolBox::CustomToolBox(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
m_mainLayout = new QVBoxLayout;
setLayout(m_mainLayout);
m_mainLayout->setContentsMargins(0,0,0,0);
m_mainLayout->setSpacing(0);
QString str = QString("delIcon.png");
QIcon icon(str);
m_toolBox1 = new MyToolBox(icon, "BTN1");
QPushButton *tmpBtn1 = new QPushButton("111");
QPushButton *tmpBtn2 = new QPushButton("222");
QPushButton *tmpBtn3 = new QPushButton("333");
m_toolBox1->addItem(tmpBtn1);
m_toolBox1->addItem(tmpBtn2);
m_toolBox1->addItem(tmpBtn3);
m_toolBox2 = new MyToolBox(icon, "BTN2");
QPushButton *tmpBtn11 = new QPushButton("111__");
QPushButton *tmpBtn22 = new QPushButton("222__");
m_toolBox2->addItem(tmpBtn11);
m_toolBox2->addItem(tmpBtn22);
m_toolBox3 = new MyToolBox(icon, "BTN3");
QPushButton *tmpBtn111 = new QPushButton("__111__");
QPushButton *tmpBtn222 = new QPushButton("__222__");
m_toolBox3->addItem(tmpBtn111);
m_toolBox3->addItem(tmpBtn222);
m_mainLayout->addWidget(m_toolBox1);
m_mainLayout->addWidget(m_toolBox2);
m_mainLayout->addWidget(m_toolBox3);
m_mainLayout->addStretch();
setGeometry(300, 300, 100, 400);
}
CustomToolBox::~CustomToolBox()
{
}