Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGridLayout: change height of a row

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.

like image 207
ettudagny Avatar asked Oct 01 '16 12:10

ettudagny


People also ask

How do I change GridLayout size?

While you can't make components span multiple rows with GridLayout, you can resize them all uniformly. Put your GridLayout on its own JPanel, and then you can use panel. setSize(x,y) to change the panel size and thus increase or decrease the size of the cells.

What is grid layout in Qt?

Grid Layout provides a way of dynamically arranging items in a grid. If the GridLayout is resized, all items in the layout will be rearranged. It is similar to the widget-based QGridLayout. All children of the GridLayout element will belong to the layout.

How are widgets added to a GridLayout?

Once you have added your layout you can start putting widgets and other layouts into the cells of your grid layout using addWidget(), addItem(), and addLayout().


1 Answers

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);
like image 116
Mike Avatar answered Oct 24 '22 14:10

Mike