QToolBox - 扩展多个项目

时间:2015-03-28 08:36:27

标签: c++ qt qwidget

我需要使QToolBox包含多个展开的项目/小部件,因此当用户点击一次时,项目展开时再次单击该项目时会崩溃,因此所有无项目都可以同时展开。如果没有足够的空间可以扩展所有项目,可以添加垂直的scroolbar。

enter image description here

任何人都有想法/解决方案我该如何做到这一点?

2 个答案:

答案 0 :(得分:3)

对于这个问题的解决方案,当我点击标题主体隐藏时,我使用标题和正文制作了一个自定义扩展小部件,更多扩展小部件和垂直布局中的间隔符的组合可能与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;

}

答案 1 :(得分:1)

如果我理解正确,您的意思是您不希望工具箱同时显示两个项目。即如果一个项目被扩展,则所有其他项目应保持关闭。

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();
}