Search code examples
c++qtqt4

Qt QHBoxLayout percentage size


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


Solution

  • 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);