wa4557
wa4557

Reputation: 1045

Swapping in a sorted TreeView

Let's say I have following code (the important stuff of a bigger class):

    self.store = Gtk.ListStore(int)
    for i in range(10):
        self.store.append(i)
    sw = Gtk.ScrolledWindow()
    sw.set_shadow_type(Gtk.ShadowType.IN)
    sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
    self.treeView = Gtk.TreeView(self.store)
    self.create_columns(self.treeView)
    sw.add(self.treeView)

def create_columns(self, treeView):
    rendererText = Gtk.CellRendererText()
    column = Gtk.TreeViewColumn("Test", rendererText, text=1)
    column.set_spacing(50)
    column.set_fixed_width(180)
    column.set_sort_column_id(1)  
    column.set_resizable(True)
    treeView.append_column(column)

def move_item_up(self):
    selection = self.treeView.get_selection()
    selections, model = selection.get_selected_rows()
    for row in selections:
        if selection.iter_is_selected(row.iter) and row.previous:
            self.store.swap(row.iter, row.previous.iter)
            break

After generating opening the window I am greeted with a column that is populated with values 1-10. If I click on a column the column is sorted according to the values. If I click once it goes from 1-10 if I click twice it show 10-1. This is perfect and exactly what I want.

So I sort my array and then I want the execute the function move-item-up . Before sorting this function works as expected, but after sorting I get an error message, such as gtk_list_store_swap: assertion 'iter_is_valid (a, store)' failed

Is there some way, to make the array "unsorted" again?

Upvotes: 0

Views: 114

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14577

Something like this:

store.set_sort_column_id (Gtk.TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID,
                          Gtk.SORT_ASCENDING);

Upvotes: 1

Related Questions