Qt QHBoxLayout percentage size

Johnny Pauling picture Johnny Pauling · Jan 28, 2013 · Viewed 21.4k times · Source

How can I maintain an aspect ratio between two QHBoxLayouts?

For instance I want a QHBoxLayout to be one third of the entire window width and the other to be two thirds of the entire window width: enter image description here

How can I achieve this? I tried messing with the size hints of the controls in them but that didn't work out

Answer

york.beta picture york.beta · Jan 28, 2013

void QSizePolicy::setHorizontalStretch(uchar stretchFactor)

Example:

QHBoxLayout* layout = new QHBoxLayout(form);

QWidget* left = new QWidget(form);
QSizePolicy spLeft(QSizePolicy::Preferred, QSizePolicy::Preferred);
spLeft.setHorizontalStretch(1);
left->setSizePolicy(spLeft);
layout->addWidget(left);

QWidget* right = new QWidget(form);
QSizePolicy spRight(QSizePolicy::Preferred, QSizePolicy::Preferred);
spRight.setHorizontalStretch(2);
right->setSizePolicy(spRight);
layout->addWidget(right);