QGridLayout's row is not removed
-
I have a
QGridLayout
that has several rows each of which having two widgets. Removing both widgets in any given row doesn't reduce theQGridLayout::rowCount()
. Tried removing the widgets using both:QLayout::removeWidget
QLayout::removeItem
- I was thinking thatQGridLayout
could somehow be still keeping theQLayoutItem
despite the fact that I've removed the widget using 1. but no
Any ideas why the layout behaves this way?
-
The reason QGridLayout::rowCount() does not decrease when removing widgets is that QGridLayout internally maintains its structure even after widgets are removed. Removing a widget using removeWidget() or removeItem() only detaches the widget but does not remove the row itself.
void removeRow(QGridLayout* layout, int row) { for (int col = layout->columnCount() - 1; col >= 0; --col) { QLayoutItem* item = layout->itemAtPosition(row, col); if (item) { layout->removeItem(item); if (QWidget* widget = item->widget()) { widget->hide(); layout->removeWidget(widget); delete widget; } delete item; } } }
-
I have a
QGridLayout
that has several rows each of which having two widgets. Removing both widgets in any given row doesn't reduce theQGridLayout::rowCount()
. Tried removing the widgets using both:QLayout::removeWidget
QLayout::removeItem
- I was thinking thatQGridLayout
could somehow be still keeping theQLayoutItem
despite the fact that I've removed the widget using 1. but no
Any ideas why the layout behaves this way?
@napajejenunedk0
Removing items/widgets does not reduce the rows or columns in aQGridLayout
, even if a row/column becomes empty.
There are several posts on this forum (and elsewhere) to the effect that you cannot delete a row/column from theQGridLayout
itself.
I think the best is @kshegunov's https://forum.qt.io/post/343288How to enforce QGridLayout to delete rows that do not contain any Widgets anymore?
You can't, you have to manually shift up (left) all cells below (right).
Also https://stackoverflow.com/a/13406780/489865
The issue is that QGridLayout::rowCount() doesn't actually return the number of rows that you can see, it actually returns the number of rows that QGridLayout has internally allocated for rows of data (yes, this isn't very obvious and isn't documented).
To get around this you can either delete the QGridLayout and recreate it
Or https://www.qtcentre.org/threads/49669-Remove-column-from-QGridLayout?p=292883#post292883
The grid layout doesn't have any methods to remove cells, so if you need them removed you need to recreate the layout,
Alternatively you can move all content below the cleared row upwards so that empty rows are at the end and then reuse those when adding new items.
-
As I was expecting. Thanks a lot for your answers. You mean that even if I delete all UI elements in row 3 out of 4 total, still rows 1, 2, 4 would contain layout items but not 3? And
QGridLayout
wouldn't move row 4 layout items to row 3 despite the fact that it would keep the maximum allocated rows count to 4? -
As I was expecting. Thanks a lot for your answers. You mean that even if I delete all UI elements in row 3 out of 4 total, still rows 1, 2, 4 would contain layout items but not 3? And
QGridLayout
wouldn't move row 4 layout items to row 3 despite the fact that it would keep the maximum allocated rows count to 4?@napajejenunedk0 Yes.
QGridLayout
is not going to do any row/column or content moving. -
@napajejenunedk0
I don't know whether it adds multiple widgets to one cell or replaces the widget there, though the fact that you have emptied out the second row should not be relevant. It will not have moved the previous third row's widgets down to the second row, as we discussed earlier. For this why don't you actually try code, it's quicker than asking and you would know for sure.... -
@napajejenunedk0
By the way, going back to your question about removing widgets. You might want to read through the somewhat detailed https://stackoverflow.com/a/19256990/489865 for your information. Although it confirms you cannot alter what rows/columns are in theQGridLayout
itself, it does show suggested code when emptying a row to keep it as small as possible, viz.layout->setRowMinimumHeight(row, 0); layout->setRowStretch(row, 0);
which you might want, I don't know?
-
@napajejenunedk0 said in QGridLayout's row is not removed:
Add widgets to 3rd row - would QGridView add them as third items or would replace the previous 3rd row items?
You can't just "add" widgets to a
QGridLayout
. You assign a position in its grid... so as long as you don't add to populated cells, it will append the widget to the grid (and extend the grid).
As @JonB said, whether you delete or modify other rows, does not matter. The rows/cols/cells are still there logically but empty and don't show anything unless you have specified some huge margin/offset.