rowCount does not update after removing widgets from grid layout
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:
Code:
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();
Re: rowCount does not update after removing widgets from grid layout
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.
Re: rowCount does not update after removing widgets from grid layout
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!
Re: rowCount does not update after removing widgets from grid layout
Well it really only increases. But also consider following:
Code:
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.
Re: rowCount does not update after removing widgets from grid layout
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!
Re: rowCount does not update after removing widgets from grid layout
Quote:
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!
Re: rowCount does not update after removing widgets from grid layout
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.
Re: rowCount does not update after removing widgets from grid layout
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.
Re: rowCount does not update after removing widgets from grid layout
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.