With QToolBox, what setting for a page is only its content size?

I am trying to find settings or size policies so that each page in the QToolBox instance QToolBox only the space needed for its content. I tried everything that I could see in the properties for both the instance and for each individual page.

Am I misinterpreting the functionality of the QToolBox widget or was there just a missing parameter?

What I'm going to look like a widget like the focus of the accordion in Qt Creator:

enter image description here

It seems that I can not get this "Sort" page to take only the size necessary to display the button and the field.

enter image description here

enter image description here

+6
source share
2 answers

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) { // Remove last spacer item if present. int count = layout->count(); if (count > 1) { layout->removeItem(layout->itemAt(count - 1)); } // Add item and make sure it stretches the remaining space. layout->addWidget(item); layout->addStretch(); } private: QVBoxLayout *layout; }; 

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.

+6
source

The example shown in Qt Designer may not use QToolBox , which behaves more like a tabbed widget in the form of tables that displays only one page at a time. An example in Qt Designer looks like a QTreeWidget with a custom drawing or style.

+3
source

Source: https://habr.com/ru/post/952986/


All Articles