Reputation: 566
I need something similar to a QToolBox
with multiple expanding items / widgets that - as opposed to a QToolBox
- supports displaying more than a single item at a time: When the user clicks on the item, it should expand; upon a second click, it should collapse: All, some or no items may be expanded at the same time.
Vertical scrollbars should be added if there is not enough space for all expanded items to be shown.
Does anyone have an idea / a solution how can I accomplish this?
Upvotes: 4
Views: 5602
Reputation: 566
For solution for this problem I made a custom expanding widget with header and body when click on header body is hidden, combination of more expanding widgets and a spacer in a vertical layout you may have the same effect with 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;
}
Upvotes: 3
Reputation: 790
If i understand you correctly, you mean you don't want the tool box to show two items at the same time. i.e. If one item is expanded, all the other items should stay closed.
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();
}
Upvotes: 1