Search code examples
pythongtk3

Python GTK +3: Sorting a treeview by clicking on column


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


Solution

  • With the help of this post, I figured a way.

    Edit the following line:

    self.treeview = Gtk.TreeView.new_with_model(self.filter)
    

    in

    self.treeview = Gtk.TreeView.new_with_model(Gtk.TreeModelSort(self.filter))
    

    and should work.