How to make the height of the subtag Qt equal?

I have QDockWidget(not floating, only closed) inside one QWidget. I have some widgets inside each QDockWidget- their heights should be equal. These internal widgets can be hidden via the context menu.

My internal widgets should be of equal height. I did it as follows:

void MyDocksPanel::redistributeSpace()
{
    QBoxLayout * lay = (QBoxLayout *)layout();
    for (int i = 0; i < lay->count(); i++)
    {
        QWidget * dock = lay->itemAt(i)->widget();
        if (dock == NULL)
            continue;

        int size = 0;
        foreach(QWidget * subWidget, dock->findChildren<QWidget*>())
            size += subWidget->isVisible() ? 1 : 0;

        if (dock->isVisible() && (size == 0))
            dock->hide();
        lay->setStretch(i, size);
    }
}

Everything works fine until I add some const elements to each QDockWidget: some horizontal scrollbars and some shortcuts ... Now my internal widgets have different sizes. But I need their heights to be very equal.

QLayoutdefines widget sizes at one level of the widget hierarchy. How can I make subscripts selected for height?

3 subspecies versus 2 shifts

First picture

3 2:

enter image description here

, :

enter image description here

5 37,37,37,28,28...

+3
2

, , . - :

dockWidgetStretch = numChildWidgets * childWidgetMinimumHeight + scrollBarHeight;

childWidgetMinimumHeight scrollBarHeight , .

EDIT: . , , , .

header.h

#include <QtGui>

class WidgetWith3Children : public QWidget
{
public:
    WidgetWith3Children()
    {
        QTextEdit *edit1 = new QTextEdit;
        QTextEdit *edit2 = new QTextEdit;
        QTextEdit *edit3 = new QTextEdit;
        QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal);
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(edit1);
        layout->addWidget(edit2);
        layout->addWidget(edit3);
        layout->addWidget(scrollBar);
        setLayout(layout);
    }
};

class WidgetWith2Children : public QWidget
{
public:
    WidgetWith2Children()
    {
        QTextEdit *edit1 = new QTextEdit;
        QTextEdit *edit2 = new QTextEdit;
        QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal);
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(edit1);
        layout->addWidget(edit2);
        layout->addWidget(scrollBar);
        setLayout(layout);
    }
};

class OuterWidget : public QWidget
{
public:
    OuterWidget()
    {
        QDockWidget *dockWidget1 = new QDockWidget;
        QDockWidget *dockWidget2 = new QDockWidget;
        dockWidget1->setWidget(new WidgetWith3Children);
        dockWidget2->setWidget(new WidgetWith2Children);
        QVBoxLayout *layout = new QVBoxLayout;

        // 71 is the height of the minimum size hint for QTextEdit
        // 30 is the height of a horizontal scrollbar (on my system)
        layout->addWidget(dockWidget1, 71 * 3 + 30);
        layout->addWidget(dockWidget2, 71 * 2 + 30);

        layout->setMargin(0);
        setLayout(layout);
    }
};

main.cpp

#include <QtGui/QApplication>
#include "header.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    OuterWidget w;
    w.show();

    return a.exec();
}
+4

, , : QDockWidget, , . , .

, , , QVBoxLayout QDockWidget. , :

QDockWidget DockWidget;
QVBoxLayout Layout = new QVBoxLayout(DockWidget);

FixedHeightWidget.setFixedHeight(10)
Layout.addWidget(FixedHeightWidget, 0);
Layout.addWidget(FirstVariableHeightWidget, 1);
Layout.addWidget(SecondVariableHeightWidget, 1);
Layout.addWidget(ThirdVariableHeightWidget, 1);

- , , .

+1

All Articles