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.
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.
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.
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().
I recommend you to read the documentation here, for information about how widgets are assigned their sizes when they are added to a layout:
- All the widgets will initially be allocated an amount of space in accordance with their
QWidget::sizePolicy()
andQWidget::sizeHint()
.- 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.
- 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.- 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.)
- 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 QListView
s, 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
.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With