Custom Objects in ListStore / TreeStore

I have a list of L objects of my class A. This class implements __str__/ __repr__, so each object has its own string representation (not necessarily unique). I have a GUI in pygtk where I have a TreeView widget with a single column. I want to fill it with string representations of objects in L, but then I want to get the selected objects as objects, not as a string. Is there a way to get TreeView to keep a list of objects, but show them as bites? If not, what is the best way to find out which objects are selected in the TreeView? The problem is also that, depending on some conditions, I may not populate the TreeView with all L, but with some sublist, so the indexes of the elements in the TreeView will not correspond to units in L.

+5
source share
3 answers

You can save the object in one column ( gobject.TYPE_PYOBJECT) and the row view in the second column, and then display only the second column in your tree view. Like here: http://www.learningpython.com/2006/09/02/extending-our-pygtk-application/

+5
source

If your Glade is 3.7.0 or later, you can enter "PyObject" (without quotes) as the column type for the ListStore. Then use set_cell_data_func to retrieve the object from the model and transfer its representation to the CellRenderer as text. There are no string columns to sync and no indexes to worry about.

+1
source

, , . .

0

All Articles