I have some QDockWidgets (not floating, only closable) inside a single QWidget.
I have some widgets inside each QDockWidget – their heights should be equal.
These inner widgets can be hidden through the context menu.
My inner widgets should have equal height. I done it this way:
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);
}
}
All works fine until I add some const elements to each QDockWidget: some horizontal scrollbars and some Labels… Now my inner widgets have different sizes. But it is necessary for me to set their heights strongly equal.
QLayout lays out widget sizes on one level of a widget’s hierarchy. How can I make height-equal subwidgets?
3 subwidgets vs 2 subwidgets

My first strategy to set stretches 3 and 2:

But, when i have added scroll bars:

Heights of my 5 widgets are equals to 37,37,37,28,28 … and thats the problem
You’re on the right track with the stretch factors, but think in terms of pixel values rather than small proportions. Try setting the stretch factor of each dock widget to this:
where
childWidgetMinimumHeightandscrollBarHeightare both expressed in pixels, and are both constants.EDIT: Here is a working example. You might have to experiment a bit to get it to work with your program, but this should be a good start.
header.h
main.cpp