Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QHBoxLayout percentage size

Tags:

c++

qt

qt4

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

like image 905
Johnny Pauling Avatar asked Jan 28 '13 11:01

Johnny Pauling


People also ask

What is stretch factor QT?

Stretch factors are used to change how much space widgets are given in proportion to one another. If we apply stretch factors to each widget, they will be laid out in proportion (but never less than their minimum size hint), e.g.

What is QVBoxLayout?

QVBoxLayout::QVBoxLayout(QWidget *parent)Constructs a new top-level vertical box with parent parent. The layout is set directly as the top-level layout for parent. There can be only one top-level layout for a widget. It is returned by QWidget::layout(). See also QWidget::setLayout().


1 Answers

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); 
like image 198
york.beta Avatar answered Sep 23 '22 22:09

york.beta