Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Scaling Layouts in Layouts (pls help)



  • I have a main window with a vertical layout and inside it are two horizontal sub layouts. So the window is split into two horizontal layouts aligned vertically. Is there an intuitive way to make one layout take up more of the screen, like a stretch factor? It seems like a trivial thing but I haven't found a way.



  • Hi, are you using ui files or are you doing in the cpp files?
    If you're using ui files, you can look at this post:
    https://forum.qt.io/topic/114009/how-to-set-different-size-of-layouts-in-qt

    If you are doing in the cpp files, you should look at:

    void QBoxLayout::setStretch(int index, int stretch)
    

    or

    void QBoxLayout::addLayout(QLayout *layout, int stretch = 0)
    


  • Hi, are you using ui files or are you doing in the cpp files?
    If you're using ui files, you can look at this post:
    https://forum.qt.io/topic/114009/how-to-set-different-size-of-layouts-in-qt

    If you are doing in the cpp files, you should look at:

    void QBoxLayout::setStretch(int index, int stretch)
    

    or

    void QBoxLayout::addLayout(QLayout *layout, int stretch = 0)
    


  • @Bonnie Thank you so much for this amazing answer. If I assign a layout to the central widget, is it possible to use a layout stretch? Or do I have to make a new layout within it first? Also, is there a way to do this with a splitter?



  • @jpie You can use a layout stretch as long as it is a box layout or grid layout.
    And yes, QSplitter also has a similar function:

    void QSplitter::setStretchFactor(int index, int stretch)
    

    Since it actually does is setting the stretch of the widget's size policy, if you want to do it in the ui file, you can just set the HorizontalStretch / VerticalStretch of the sizePolicy in a splitter's child widget.


Log in to reply