开发者

Custom objects in ListStore/TreeStore

I have a list L of objects of my class A. This class implements __str__/__repr__, so each object has it's own string representation (not necessary unique). I have a GUI in pygtk, where I have a TreeView widget with only one column. I 开发者_如何学JAVAwant to populate it with string representations of the objects in L, but then I want to get selected items as objects, not as string. Is there a way to make TreeView to store list of objects, but display them as stings? If not, then what is the best way to know what objects are selected in the TreeView? The problem also is that depending on some conditions I can populate TreeView not with the whole L, but with some sublist of it, and so indexes of items in TreeView won't correspond to ones in L.


You could store the object in one column (gobject.TYPE_PYOBJECT) and the string representation in a second column, and then only display the second column in your treeview. Similar to what's done here: http://www.learningpython.com/2006/09/02/extending-our-pygtk-application/


If your Glade is 3.7.0 or newer you can type "PyObject" (without the quotes) as the column type for you ListStore. Then use set_cell_data_func to retrieve an object from the model and pass its representation to the CellRenderer as text. No string columns to synchronize and no indexes to worry about.


If the strings are unique you can use a dictionary to link the strings with the objects by using the strings as keys. In this case you can find the objects by its string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜