Emilio Conte
Emilio Conte

Reputation: 1135

Sort a column in a treeview by default or programmatically

I know my question has already been asked here:

How to Programmatically Sort TreeView

But the link given doesn't link nothing and I'm still not able to have my treeview sorted when my window is showed.

Here is my code:

    treeview = Gtk.TreeView(model=liststore)
    col = renderer_text('Nom', 1, store=liststore, sortable=True)
    col.set_sort_order(Gtk.SortType.ASCENDING) #??????????????????????????
    treeview.append_column(col)

def renderer_text(title, col, editable=False, store=None, sortable=None):
    render = Gtk.CellRendererText()
    if editable:
        render.set_property('editable', True)
        render.connect('edited', text_edited, store, col)
    column = Gtk.TreeViewColumn(title, render, text=col)
    if sortable:
        column.set_sort_column_id(col)
    return column

The set_sort_order(Gtk.SortType.ASCENDING) would have been fantastic but It doesn't sort nothing in my case !

Upvotes: 2

Views: 3901

Answers (2)

mariotomo
mariotomo

Reputation: 9728

If you're fine with simple lexicographic sorting, and if you're particularly lazy, and imagine you're developing your interface with glade, maybe you can use the following snippet from my own code (sorry I'm still stuck with gtk2).

The code in this answer looks similar to ptomato's but we're in a different part of the data structures, it's just the methods that are named the same. I'm just using plain TreeView and plain ListStore.

gtk.TreeViewColumn widgets have a set_sort_column_id convenience method that sets up almost everything one needs.

Here the _tvc suffix means it's a gtk.TreeViewColumn object. As said, all is created given a glade file:

    self.view.widgets.use_tvc.set_sort_column_id(0)
    self.view.widgets.filename_tvc.set_sort_column_id(1)
    self.view.widgets.accno_tvc.set_sort_column_id(2)
    self.view.widgets.binomial_tvc.set_sort_column_id(3)
    self.view.widgets.iseditable_tvc.set_sort_column_id(5)

And here you have a snippet from the glade file, where I'm defining the accno_tvc column, putting a CellRendererText in it, specifying the columns in the ListStore that hold the data for some of the columns' attributes.

<object class="GtkTreeViewColumn" id="accno_tvc">
  <property name="title" translatable="yes">acc.nr.</property>
  <property name="clickable">True</property>
  <child>
    <object class="GtkCellRendererText" id="accno_crtext">
      <signal name="edited" handler="on_cellrenderertext_edited" swapped="no"/>
    </object>
    <attributes>
      <attribute name="editable">5</attribute>
      <attribute name="text">2</attribute>
    </attributes>
  </child>
</object>

As you can see the python code, where it specify which data column to use when sorting on the accno_tvc gui column, repeats the same index 2 as the glade file uses for the renderer's text attribute.

Upvotes: 0

ptomato
ptomato

Reputation: 57854

It's hard to see what's going wrong without seeing more of your code, but I'll wager exactly the same thing is happening as in the other question: you need to wrap your tree model in a Gtk.TreeModelSort.

sorted_model = Gtk.TreeModelSort(model=liststore)
sorted_model.set_sort_column_id(1, Gtk.SortType.ASCENDING)
treeview = Gtk.TreeView(model=sorted_model)

Note that 1 is the index into your model's columns in this case, not the visible treeview columns.

Upvotes: 10

Related Questions