QGridLayout: change height of a row

11,455

I recommend you to read the documentation here, for information about how widgets are assigned their sizes when they are added to a layout:

  1. All the widgets will initially be allocated an amount of space in accordance with their QWidget::sizePolicy() and QWidget::sizeHint().
  2. If any of the widgets have stretch factors set, with a value greater than zero, then they are allocated space in proportion to their stretch factor.
  3. If any of the widgets have stretch factors set to zero they will only get more space if no other widgets want the space. Of these, space is allocated to widgets with an Expanding size policy first.
  4. Any widgets that are allocated less space than their minimum size (or minimum size hint if no minimum size is specified) are allocated this minimum size they require. (Widgets don't have to have a minimum size or minimum size hint in which case the stretch factor is their determining factor.)
  5. Any widgets that are allocated more space than their maximum size are allocated the maximum size space they require. (Widgets do not have to have a maximum size in which case the stretch factor is their determining factor.)

Assuming that you haven't changed the sizePolicy for the QListViews, then, they all have the default sizePolicy of Expanding. That means, you just need to set suitable stretch factors for the rows in the QGridLayout.

If you are using the Qt designer, just click on the QGridLayout (or the widget that has the QGridLayout), and in the Property Editor set layoutRowStretch to something like 3,1,2.

Property Editor screenshot

3,1,2 will set the third row to fill up twice the height of empty space taken by the second row, while the first row will fill up three times the height of the empty space taken by the second row.

If you want to do that without the designer, you can use the setRowStretch, something like this:

gridLayout.setRowStretch(0, 3);
gridLayout.setRowStretch(1, 1);
gridLayout.setRowStretch(2, 2);
Share:
11,455
ettudagny
Author by

ettudagny

Updated on June 29, 2022

Comments

  • ettudagny
    ettudagny almost 2 years

    I'm new at qt and now my window looks like this:

    *---------* *---------* *---------* *---------* 
    |ListView1| |ListView2| |ListView3| |ListView4| 
    |         | |         | |         | |         |
    *---------* *---------* *---------* *---------* 
    *---------------------------------------------*
    |                                             |
    |                  ListView5                  |
    |                                             |
    *---------------------------------------------*    
    *---------------------------------------------*
    |                  GridLayout2                |
    |                                             |
    *---------------------------------------------*
    

    The problem is that second row (ListView5) is higher than first and third rows, while I want first row to be higher than third row, which should be higher than second row. Is there a way to do it? Or should I use something else insted of gridlayout? Thanks.