Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

tut-treeview-model-remove

Removing Rows

Rows can easily be removed with Gtk::ListStore#remove and Gtk::TreeStore#remove. The removed row will automatically be removed from the tree view as well.

Removing multiple rows at once can be a bit tricky at times, and requires some thought on how to do this best. For example, it is not possible to traverse a store with Gtk::TreeModel#each, check in the code block whether the given row should be removed and then just remove it by calling one of the stores' remove methods. This may not work, because the model is changed from within the foreach loop, which might suddenly invalidate formerly valid tree iters in the each method, and thus lead to unpredictable results.

Here is an example for an alternative approach when removing multiple rows in one go (here we want to remove all rows from the store that contain persons that have been born after 1980, but it could just as well be all selected rows):

underage = []
liststore.each do |model,path,iter|
  (iter[2] > 1980) and underage.push(Gtk::TreeRowReference.new(model,path))
end

underage.each do |rowref|
  (path = rowref.path) and liststore.remove(liststore.get_iter(path))
end

Gtk::ListStore#clear and Gtk::TreeStore#clear come in handy if you want to remove all rows.

Having learned how to add, manipulate, and retrieve data from a store, the next step is to get that data displayed in a Gtk::TreeView widget.

Last modified:2005/05/03 02:29:58
Keyword(s):
References:[tut-treeview-selection-handle] [tut-treeview]