I am trying to remove multiple rows from a Gtk.TreeStore:
data_selection = data_treeview.get_selection()
data_model, data_row_list = data_selection.get_selected_rows()
treeiter_list = []
for treepath in data_row_list:
row = data_model[treepath]
treeiter_list.append(data_model.get_iter(row))
for treeiter in treeiter_list:
data_treestore.remove(treeiter)
This works if I only select one row. As soon as I select more than one I get this error:
Gtk-CRITICAL **: gtk_list_store_remove: assertion 'iter_is_valid (iter, list_store)' failed
Do the treeiters become invalid after the first row is deleted?
Do the treeiters become invalid after the first row is deleted?
Yes. And also the tree paths change if one row is deleted.
The key is to delete the tree paths in reverse order, so the paths remain valid. Also do not store the iters, use the paths instead and call for an iter inside the loop, a double loop is not required.
Key part of the example:
selection = self.view.get_selection()
model, paths = selection.get_selected_rows()
for p in reversed(paths):
itr = model.get_iter(p)
model.remove(itr)
Full example can be found at:
https://gist.github.com/carlos-jenkins/c4fedad66169b75424a0
Note that this will work if the model isn't a sorted model. If it is, you will need to call
convert_iter_to_child_iter
in order to remove it.