Nik
Nik

Reputation: 759

Getting the row number of Gtk TreeView

I have a GTk TreeView called MyTree with the following data as presented below,

DTime  ATime Transfer Platform

14:30    15:20    0       2a

14:50    15:40    0       14b

15:00    16:00    2       3a

As you can see I have 3 rows with 4 columns. So I need to get the selected row. I did this by,

selection = MyTree.get_selection()
selection.set_mode(Gtk.SelectionMode.BROWSE)
model, iter = selection.get_selected()

At this point it returns the tree iter which points at the currently selected row. This is all fine. However I am interested in knowing whether iter is pointing at row 0, 1 or 2.

I hope I have made this clear. I need the row index and not the row iter. How do I get the row number?

Upvotes: 4

Views: 4396

Answers (1)

Charles Brunet
Charles Brunet

Reputation: 23160

Since you are in BROWSE selection mode, you know there is only one selected row. Then, you can get the path to the first selected item doing

path = iter.get_selected_rows()[0]

Then, if your tree has only one level (e.g. it is not a nested tree), you can get the index from the path like this

index = path.get_indices()[0]

It seems complicated, but this is because you selection could contains many rows (this is why it returns a list) and because the tree can have many levels (this is why it returns a path).

Upvotes: 5

Related Questions