In Gtk, how do I search a ListStore for the row containing a particular value?

不羁岁月 提交于 2019-12-10 23:07:02

问题


I have a ListStore modeling a list of Tags. This list may change apart from the ListStore. What I'd like to do is listen to the TagRemoved event in my TagList class, and remove the Tag from the ListStore when the event is triggered. However, I can't seem to find a way to search a ListStore for the row containing a given Tag, so that I can remove it.

Is there any way to do this?


回答1:


A GtkListStore implements the GtkTreeModel interface, which contains the tree traversal operations you want. As far as I know, there is no convenience API for searching a list/tree store, so you'll have to roll your own.

Since you're simply iterating over a GtkListStore, you can ignore all of the API dealing with child/parent relations, and simply use gtk_tree_model_iter_first() and gtk_tree_model_iter_next() to traverse the list.

Alternatively, if you already know the index of the removed tag in the store (e.g., from your TagRemoved event), you can turn that into a GtkTreePath and use gtk_tree_model_get_iter() to retrieve the row in question directly without searching.




回答2:


GtkListStore is internally implemented as a linked list so you should scan the model by yourself.



来源:https://stackoverflow.com/questions/2214431/in-gtk-how-do-i-search-a-liststore-for-the-row-containing-a-particular-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!