QToolBox - 展开多个项目
QToolBox - Expanding multiple items
我需要使 QToolBox
具有多次扩展 items/widgets 这样当用户单击一次该项目展开时再单击一次该项目折叠,因此所有 none 项目都可以同时扩大。如果没有足够的 space 来展开所有项目,可以添加垂直滚动条。
任何人都idea/solution我怎样才能完成这个?
如果我没理解错的话,你的意思是你不希望工具箱同时显示两个项目。即如果一个项目被展开,所有其他项目应该保持关闭。
toolbox.h
#ifndef TOOLBOX_H
#define TOOLBOX_H
#include <QDialog>
#include <QToolBox>
#include <QLabel>
#include <QVBoxLayout>
class ToolBox : public QDialog
{
Q_OBJECT
public:
explicit ToolBox(QWidget *parent = 0);
~ToolBox();
private:
QToolBox *toolBox;
QLabel *label1;
QLabel *label2;
QLabel *label3;
};
#endif // TOOLBOX_H
toolbox.cpp
#include "toolbox.h"
#include "ui_toolbox.h"
ToolBox::ToolBox(QWidget *parent) : QDialog(parent) {
label1 = new QLabel("first item");
label2 = new QLabel("second item");
label3 = new QLabel("third item");
toolBox = new QToolBox;
toolBox->addItem(label1, "page 1");
toolBox->addItem(label2, "page 2");
toolBox->addItem(label3, "page 3");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(toolBox);
setLayout(layout);
}
ToolBox::~ToolBox() {
delete toolBox;
delete label1;
delete label2;
delete label3;
}
main.cpp
#include "toolbox.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ToolBox *window = new ToolBox;
window->show();
return a.exec();
}
为了解决这个问题,我制作了一个带有 header 和 body 的自定义扩展小部件,当单击 header 时 body 被隐藏,更多扩展小部件的组合和垂直布局中的间隔符可能与 QToolbox
具有相同的效果
expandwidget.h:
#ifndef EXPANDWIDGET_H
#define EXPANDWIDGET_H
#include <QWidget>
#include <QLabel>
class ExpandWidgetHeader : public QLabel
{
Q_OBJECT
public:
explicit ExpandWidgetHeader(QWidget *parent = 0);
~ExpandWidgetHeader();
signals:
void headerClicked(bool hide);
private:
virtual void mousePressEvent(QMouseEvent * event);
bool m_isHidden;
// QLabel* headerTitle;
};
//================================================================
class ExpandWidget : public QWidget
{
Q_OBJECT
public:
explicit ExpandWidget(QWidget *parent = 0);
public slots:
void hideBody(bool hide);
public:
ExpandWidgetHeader *Header;
QWidget *Body;
};
#endif // EXPANDWIDGET_H
expandwidget.cpp:
#include "expandwidget.h"
ExpandWidget::ExpandWidget(QWidget *parent) :
QWidget(parent)
{
this->setStyleSheet("border: 1px solid #000000; background: #C4C4C1");
setMinimumSize(QSize(330, 100));
Header = new ExpandWidgetHeader(this);
Body = new QWidget(this);
Body->setGeometry(QRect(0, 25, 330, 100));
connect(Header,SIGNAL(headerClicked(bool)),this,SLOT(hideBody(bool)));
}
void ExpandWidget::hideBody(bool hide)
{
if (!hide)
{
Body->hide();
this->setMinimumHeight(Header->height());
}
else
{
this->setMinimumHeight(Body->height()+Header->height());
Body->show();
}
}
//=========================================================================
ExpandWidgetHeader::ExpandWidgetHeader(QWidget *parent) :
QLabel(parent)
{
m_isHidden=false;
this->setText("Text");
this->setStyleSheet("border: 1px solid #000000; background: #898983");
this->setGeometry(QRect(0, 0, 330, 25));
this->setAlignment(Qt::AlignCenter);
}
ExpandWidgetHeader::~ExpandWidgetHeader()
{
}
void ExpandWidgetHeader::mousePressEvent(QMouseEvent *event)
{
if (!m_isHidden)
{
emit headerClicked(m_isHidden);
}
else
{
emit headerClicked(m_isHidden);
}
m_isHidden = !m_isHidden;
}
我需要使 QToolBox
具有多次扩展 items/widgets 这样当用户单击一次该项目展开时再单击一次该项目折叠,因此所有 none 项目都可以同时扩大。如果没有足够的 space 来展开所有项目,可以添加垂直滚动条。
任何人都idea/solution我怎样才能完成这个?
如果我没理解错的话,你的意思是你不希望工具箱同时显示两个项目。即如果一个项目被展开,所有其他项目应该保持关闭。
toolbox.h
#ifndef TOOLBOX_H
#define TOOLBOX_H
#include <QDialog>
#include <QToolBox>
#include <QLabel>
#include <QVBoxLayout>
class ToolBox : public QDialog
{
Q_OBJECT
public:
explicit ToolBox(QWidget *parent = 0);
~ToolBox();
private:
QToolBox *toolBox;
QLabel *label1;
QLabel *label2;
QLabel *label3;
};
#endif // TOOLBOX_H
toolbox.cpp
#include "toolbox.h"
#include "ui_toolbox.h"
ToolBox::ToolBox(QWidget *parent) : QDialog(parent) {
label1 = new QLabel("first item");
label2 = new QLabel("second item");
label3 = new QLabel("third item");
toolBox = new QToolBox;
toolBox->addItem(label1, "page 1");
toolBox->addItem(label2, "page 2");
toolBox->addItem(label3, "page 3");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(toolBox);
setLayout(layout);
}
ToolBox::~ToolBox() {
delete toolBox;
delete label1;
delete label2;
delete label3;
}
main.cpp
#include "toolbox.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ToolBox *window = new ToolBox;
window->show();
return a.exec();
}
为了解决这个问题,我制作了一个带有 header 和 body 的自定义扩展小部件,当单击 header 时 body 被隐藏,更多扩展小部件的组合和垂直布局中的间隔符可能与 QToolbox
expandwidget.h:
#ifndef EXPANDWIDGET_H
#define EXPANDWIDGET_H
#include <QWidget>
#include <QLabel>
class ExpandWidgetHeader : public QLabel
{
Q_OBJECT
public:
explicit ExpandWidgetHeader(QWidget *parent = 0);
~ExpandWidgetHeader();
signals:
void headerClicked(bool hide);
private:
virtual void mousePressEvent(QMouseEvent * event);
bool m_isHidden;
// QLabel* headerTitle;
};
//================================================================
class ExpandWidget : public QWidget
{
Q_OBJECT
public:
explicit ExpandWidget(QWidget *parent = 0);
public slots:
void hideBody(bool hide);
public:
ExpandWidgetHeader *Header;
QWidget *Body;
};
#endif // EXPANDWIDGET_H
expandwidget.cpp:
#include "expandwidget.h"
ExpandWidget::ExpandWidget(QWidget *parent) :
QWidget(parent)
{
this->setStyleSheet("border: 1px solid #000000; background: #C4C4C1");
setMinimumSize(QSize(330, 100));
Header = new ExpandWidgetHeader(this);
Body = new QWidget(this);
Body->setGeometry(QRect(0, 25, 330, 100));
connect(Header,SIGNAL(headerClicked(bool)),this,SLOT(hideBody(bool)));
}
void ExpandWidget::hideBody(bool hide)
{
if (!hide)
{
Body->hide();
this->setMinimumHeight(Header->height());
}
else
{
this->setMinimumHeight(Body->height()+Header->height());
Body->show();
}
}
//=========================================================================
ExpandWidgetHeader::ExpandWidgetHeader(QWidget *parent) :
QLabel(parent)
{
m_isHidden=false;
this->setText("Text");
this->setStyleSheet("border: 1px solid #000000; background: #898983");
this->setGeometry(QRect(0, 0, 330, 25));
this->setAlignment(Qt::AlignCenter);
}
ExpandWidgetHeader::~ExpandWidgetHeader()
{
}
void ExpandWidgetHeader::mousePressEvent(QMouseEvent *event)
{
if (!m_isHidden)
{
emit headerClicked(m_isHidden);
}
else
{
emit headerClicked(m_isHidden);
}
m_isHidden = !m_isHidden;
}