Unfortunately, you cannot do this directly, because it will cover all the space that is not visible to the header widgets. You can emulate what you want by setting a fixed height on the QToolBox if you know the exact height of your page. But you do not want to do this in practice.
If you need the behavior you are asking for, you need to write your own custom control. It should not be difficult. Use a QVBoxLayout and fill it with the elements of a custom class, call it ToolItem , which is a QWidget with a title (possibly a button to show / hide) and another QWidget to display content that is either visible or not.
The following very simple example will switch the visibility of the ToolItem when clicked. And only when the visible will occupy some space.
class ToolItem : public QWidget { public: ToolItem(const QString &title, QWidget *item) : item(item) { QVBoxLayout *layout = new QVBoxLayout; layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(new QLabel(title)); layout->addWidget(item); setLayout(layout); item->setVisible(false); } protected: void mousePressEvent(QMouseEvent *event) { item->setVisible(!item->isVisible()); } private: QWidget *item; }; class ToolBox : public QWidget { public: ToolBox() : layout(new QVBoxLayout) { setLayout(layout); } void addItem(ToolItem *item) {
And simple use:
QWidget *window = new QWidget; window->setWindowTitle("QToolBox Example"); QListWidget *list = new QListWidget; list->addItem("One"); list->addItem("Two"); list->addItem("Three"); ToolBox *toolBox = new ToolBox; toolBox->addItem(new ToolItem("Title 1", new QLabel("Some text here"))); toolBox->addItem(new ToolItem("Title 2", list)); toolBox->addItem(new ToolItem("Title 3", new QLabel("Lorem Ipsum.."))); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(toolBox); window->setLayout(layout); window->resize(500, 500); window->show();
Now you can configure it to look like a QToolBox , if necessary.
Please feel free to ask additional questions.
source share