How can I remove all rows of a GtkGrid?

,

I want to clean all the rows into a grid. There are any method different to gtk_grid_remove_row() ?

If you want to remove all children in a GtkGrid, then you can use something like:

GtkWidget *iter = gtk_widget_get_first_child (grid);
while (iter != NULL) {
  GtkWidget *next = gtk_widget_get_next_sibling (iter);
  gtk_grid_remove (grid, iter);
  iter = next;
}

Alternatively, you can just remove the grid from its parent, and create a new grid.

I think this is a perfect case for improving API.

Sure; feel free to open a merge request about it.

The truth is that scaffolding widgets, like Box and Grid, are more often built and never touched again; application developers use Stack, Overlay, or other layered widgets to switch between different views.

Tbh, not providing such an obvious operation is simply un-intuitive. This is a standard operation on every container I am aware of. Nobody loops over container to remove all of its elements.
Just sayin’

There was no such API in GTK3 either: you either destroyed the container, or you retrieved the children list and looped over them.

The issue is designing an API that is both useful and that nudges people towards the supported/intended use case. The API still provides you with a way to achieve what you want, at the cost of asking yourself: is it really what you want? Should you be using this container at all? Maybe there are better options at your disposal.

As I said: feel free to write a merge request that implements a gtk_grid_remove_all(); we’ll review it just like any other API addition.

OK, so perhaps I can learn something new here.
What is the intended use for a grid when I need to repopulate the grid with completely new set of widgets.
In the beginning of the running application I read some let’s say input from a file and that input tells me the number of widgets that grid needs to be populated with.
Then, the user can select new number and that new number is the number that grid needs to be populated with. The number can be bigger than the current or smaller. The number can also be zero.
What is the suggested and intended use for such scenario?

It depends. As I’ve said above, there are various scenarios. If you are using Grid as a scaffolding container, with multiple states, each containing a fixed sets of UI elements, then you probably want to use a GtkStack and create a page for each state, with each page containing a Grid and its related UI elements.

If you are building a UI dynamically, with the ability for a user to change the layout, then you probably want to have a custom container—possibly using a GtkGridLayout to handle the layout of the children; then you can provide your own API to add and remove columns and rows.

There’s also the option of recreating a GtkGrid from scratch, every time you manipulate the rows/columns; it’s not any less efficient than removing all children of a grid widget and replacing them with some other list of children—even if GTK provided that as an API.

The important things to consider are these:

  • GTK is not ever going to provide all possible widgets for all possible use cases; you may very well end up writing your own widgets, something we actually encourage people to do especially with GTK4
  • the existing container widgets are generally considered static “scaffolding” for UIs; for dynamic UI we recommend using widgets like GtkListView and GtkGridView, which use models and “recycle” items to scale to a range of millions of items.

Yes, GtkListView seems like correct direction.
Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.