Reputation: 121
I want to be able to sort a treeview by column by clicking on a column. I'm simply using the popular example taken from the docs as reference (https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html):
self.treeview = Gtk.TreeView.new_with_model(self.filter)
for i, column_title in enumerate(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
self.treeview.append_column(column)
column.set_sort_column_id(i)
except I get the error:
(test.py:26081): Gtk-CRITICAL **: 12:35:03.856: gtk_tree_sortable_get_sort_column_id: assertion 'GTK_IS_TREE_SORTABLE (sortable)' failed
(test.py:26081): Gtk-CRITICAL **: 12:35:03.856: gtk_tree_sortable_has_default_sort_func: assertion 'GTK_IS_TREE_SORTABLE (sortable)' failed
(test.py:26081): Gtk-CRITICAL **: 12:35:03.856: gtk_tree_sortable_set_sort_column_id: assertion 'GTK_IS_TREE_SORTABLE (sortable)' failed
Upvotes: 3
Views: 887
Reputation: 11
The answer given by Valentino will only allow the user to sort the tree by default sort method. If you need to sort with a custom function you can do so. by doing this.
Create a Gtk.TreeModelSort object with filter and assign a sort fucntion.
sorted_model = Gtk.TreeModelSort(model=self.filter)
sorted_model.set_sort_func(0, self.sort_tree, None)
Then Set the treeview with this sort model.
self.tree_view = Gtk.TreeView(model=sorted_model)
This worked for me.
Upvotes: 0