Johannes Jordens <[EMAIL PROTECTED]> writes: > Question: How do I get an iter to the row preceding the selected one? > alternatively, how do I implement moving a row upwards?
In a ListStore, you can do something like this: path = listiter.get_path() row = path[0] assert row > 0 prev_row = row - 1 prev_iter = listmodel.get_iter(prev_row) This makes use of the fact that in a ListStore the path is a 1-tuple that gives the row number. Since we typically want to operate on the selected row, I use an up button 'clicked' handler that looks something like this: def on_up_button__clicked(button, my_model): """Move the selected row up. """ selection = my_model.get_selection() model, selected = selection.get_selected() assert not selected is None path = model.get_path(selected) row = path[0] assert row > 0 this = model[row][0] prev = model[row-1][0] model[row-1][0] = this model[row][0] = prev selection.select_path(row-1) (Here I have passed the model as an extra argument to the handler. Use any convenient means to get the treeview selection.) The code works for a 1-column liststore. You need to make the obvious modifications if the liststore has more columns. [We would like to use the ListStore.move_before() and ListStore.move_after() methods, but since they are gtk+ 2.2 API additions they aren't wrapped yet by pygtk.] I make liberal use of the pygtk binding's Python list-like API; you can use model.get_value() and model.set_value() if you prefer. (This is explained in the excellent pygtk FAQ.) The assertions check to make sure that the move up makes sense since I use a 'changed' handler on the selection to make the up button insensitive when the selected row can't be moved up. If this function might be called when the move can't be made you can instead simply return rather than causing an assertion failure. In case you're interested, the selection 'changed' handler method that I use looks like this: def selection_changed(self, selection): """Set button sensitivities after treeview selection changes. """ model, selected = selection.get_selected() has_selection = bool(selected) self.remove_button.set_sensitive(has_selection) path = has_selection and model.get_path(selected) self.up_button.set_sensitive(path and path[0] > 0) self.down_button.set_sensitive(path and path[0] < len(model)-1) Here again pygtk's nifty Python list API for ListStore helps by letting us easily find the number of rows in the model with a simple len(model). _______________________________________________ pygtk mailing list [EMAIL PROTECTED] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/