Get new selection in a GtkTreeView during the signal
Asked Answered
H

1

5

I want to detect whenever the selection of my gtk.TreeView changes and, when it does, to call a function w/ this information. The only way I've found to do it so far is to attach to all these signals:

...
    self.sitterView.connect("cursor-changed", self.selectionChanged)
    self.sitterView.connect("unselect-all", self.selectionChanged)
    self.sitterView.connect("toggle-cursor-row", self.selectionChanged)
    self.sitterView.connect("select-all", self.selectionChanged)    
...
def selectionChanged(self, treeview):
    foo(self.sitterView.get_selection().get_selected())

However, it seems like the selection I get from the callback is "delayed". That is, it shows the selection after the previous callback had completed. For example, if I constantly CTRL+click on a row, when the row goes from deselected to selected, foo is given no selection, and when the row goes from selected to deselected, it is given a selection. If I call get_selection().get_selected() a second later, though, I get the right selection. Any idea how to deal w/ this?

Hyperspace answered 16/9, 2010 at 23:10 Comment(0)
H
7

I'm not sure what toggle-cursor-row does (the documentation is frustratingly empty), but I think that's the wrong signal to handle.

Instead, you should connect to the GtkTreeSelection changed signal. It should take care of all selection change events, so you don't need to connect to the other signals either.

Huh answered 17/9, 2010 at 3:55 Comment(3)
oh awesome! i didn't realize that the selection object wouldn't change, and that it had signals of its own. i think that's what I'm looking for.Hyperspace
Is there a way to connect this signal from Glade?Whitehurst
umpirsky: Not that I can tell. There is a Tree Selection object in Glade (under Tree Model) but I can't find a way to use it in a treeview.Huh

© 2022 - 2024 — McMap. All rights reserved.