如何将 gtk.ListStore 项目与我自己的模型相关联
我有一个项目对象列表,显示在 GtkTreeView 中。当用户双击 TreeView 中的项目行时,我尝试打开一个包含项目详细信息的对话框。
现在,我通过 get_selection() 从 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不适用于默认型号。您可以尝试使用专为在后端和演示中使用相同对象而编写的 Py-gtktree 模型。
顺便说一句,它的文档概述了另一种方法来使其与标准模型一起工作(即根本不使用 Py-gtktree),但我不会称其为优雅。
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.
我最终做的是扩展 gtk.ListStore 并使用我的自定义列表。我还劫持了append()方法,这样它不仅会将[str、str等]附加到ListStore中,而且还会将扩展ListStore的类的自定义列表属性内的实际模型附加到ListStore中。
然后,当用户双击该行时,我通过 ListStore 中的行索引获取请求的模型,该索引对应于自定义列表中的模型索引。
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.