问题
I have a ListStore
modeling a list of Tag
s. 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