开发者

How to correlate gtk.ListStore items with my own models

开发者 https://www.devze.com 2023-01-03 03:02 出处:网络
I have a list of Project objects, that I display in a GtkTreeView. I am trying to open a dialog with a Project\'s details when the user double-clicks on the item\'s row in the TreeView.

I have a list of Project objects, that I display in a GtkTreeView. I am trying to open a dialog with a Project's details when the user double-clicks on the item's row in the TreeView.

Right now I get the selected value from the TreeView (which is the name of the Project) via get_selection(), and search for that Project by name in my own list to corelate the selection with my own model.

However, this doesn't feel quite right (plus, it assumes that a Project's name is unique), and开发者_开发问答 I was wondering if there is a more elegant way of doing it.


Not with the default models. You could try using Py-gtktree models written specifically to use the same objects in backend and presentation.

Its documentation outlines an alternative way to make this work with standard models (i.e. without using Py-gtktree at all), by the way, but I wouldn't call it elegant.


What I ended up doing was extending gtk.ListStore and use my custom list. I also hijacked the append() method so that not only it will append a [str, str, etc] into the ListStore, but also the actual model inside a custom list property of the class that extends ListStore.

Then, when the user double clicks the row, I fetch the requested model by the row's index in the ListStore, which corresponds to the model's index in the custom list.

0

精彩评论

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