This is yet another simple HOWTO for personal reference, but I document this in the hope that it will be useful for others as well.
As I mentioned before, the gtk.TreeView widget is one of the most tortuous Gtk widgets ever created. Using and manipulating it involves quite a few non-obvious techniques. Here's the code to deleting multiple selected items in a gtk.TreeView widget.
In the below example the widgets are created by Glade/GtkBuilder so I call the
get_object () method of the GtkBuilder object to get the widgets.
def remove_selected_items (self):
# get the file treeview and liststore
file_view = self.ui.get_object ("tree_files")
file_list = self.ui.get_object ("filelist")
# get the selected rows as paths
sel_model, sel_rows = file_view.get_selection ().get_selected_rows ()
# store the treeiters from paths
iters = []
for row in sel_rows:
iters.append ( file_list.get_iter (row) )
# remove the rows (treeiters)
for i in iters:
if i is not None:
file_list.remove (i)
As you can see, it involves the following steps:
- Get the TreeSelection object associated with the
TreeView and call get_selected_rows () to get the row model and selected paths.
- Convert each of the selected paths into
TreeIters.
- Now call the method
remove () method on each of the selected items in the ListStore or TreeModel object.
Note that directly trying to remove paths in the first step will lead to an error because once a path is deleted, the remaining paths might point to a non-existent node.
2 comment(s)
Leave a comment »It works perfectly fine
Clearly treemodel are quite a pain in the a** to manipulate.
Comment by TeT (visitor) on 17 Sep 2010 @ 20:13 IST #
Comment by Hari (blog owner) on 17 Sep 2010 @ 20:44 IST #