PDA

View Full Version : rowCount does not update after removing widgets from grid layout



Ishmael
2nd June 2010, 23:46
I'm trying to dynamically remove/delete rows from a QGridLayout, but I can't seem to get the rowCount() property to update. This is what I'm trying:



qDebug()<<"Before: "<<my_grid_layout->rowCount();

QLayoutItem *child = my_grid_layout->takeAt(row_index);

my_grid_layout->removeItem(child);
my_grid_layout->removeWidget(child->widget());

delete child->widget();
delete child;

my_grid_layout->update();

qDebug()<<"After: "<<my_grid_layout->rowCount();

Lykurg
3rd June 2010, 00:35
if you use takeAt() you don't have to call removeItem() and removeWidget(). About the row count I guess it is only increasing not decreasing. But I am not sure about it right now.

Ishmael
3rd June 2010, 01:04
Thanks for the reply. I suppose I'll just have to keep track of the row count myself. The rowCount() property is basically useless if it only increases!

Lykurg
3rd June 2010, 01:23
Well it really only increases. But also consider following:
QGridLayout l;
QWidget *w = new QWidget;
l.addWidget(w, 3, 0);
qWarning() << l.rowCount();
it will return 4. So the row property is not what you are expecting!
And do you need the grid functionality because a QVBoxLayout should work like you expect it.

Ishmael
3rd June 2010, 02:36
Actually, in your example, I think rowCount() returns exactly what you would expect. You added a widget to the 4th row (index 3) so it returns 4. The problem is, if you remove that same widget, it still says 4! Anyway, your suggestion to use a QVBoxLayout is a good one. I'll try that. Thanks!

JD2000
3rd June 2010, 20:01
You added a widget to the 4th row (index 3) so it returns 4. The problem is, if you remove that same widget, it still says 4! there may be more than one item in the row, so deleting it just because an item was removed is probably not a good idea!

dmateer
2nd September 2010, 20:27
But this behavior is a bug, or at least incredibly non-intuitive. If I add a single widget at row 3, then delete the only widget in that row, the row count should most certainly not include that row. As it is right now, is is impossible to reduce the size of a grid if at any point an item was added to a row.

ChrisW67
4th September 2010, 06:10
What do you propose it does if there a full rows of widget at 0, 1, 2, and 4 and only one widget in row 3, which you then remove? Should row three be removed and the widgets in row four be renumbered and the layout resized? How about if taking out cell in row 3 also makes its column empty? Remove it? Doing this automatically potentially breaks any code that references the grid cells by row and column.

VeeraQtc09
5th May 2021, 17:41
If it suits you, you can rather use my_grid_layout->count() and divide it by the number of columns in the layout if the number of columns is known and not going to change.