我有一个类似的问题,并在C#中的这个答案是有帮助的,但在C中的最终解决方案看起来非常不同。我在这里发布,因为它是谷歌搜索的第一个。
基本上,如果您正在查看GTK组合框并使用GTK树模型,并且希望获取信息,则必须正确使用iter模式。 Python和C#等其他语言的包装使得它更容易一些,但对于那些仍然使用C与GTK的人来说,这里是解决方案:
假设你有一个平坦的gtk组合框,并且你只需要获取某些东西这一点,你可以使用类似这样的C:
int set_combo_box_text(GtkComboBox * box, char * txt)
{
GtkTreeIter iter;
GtkListStore * list_store;
int valid;
int i;
list_store = gtk_combo_box_get_model(box);
// Go through model's list and find the text that matches, then set it active
i = 0;
valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(list_store), &iter);
while (valid) {
gchar *item_text;
gtk_tree_model_get (GTK_TREE_MODEL(list_store), &iter, 0, &item_text, -1);
printf("item_text: %s\n", item_text);
if (strcmp(item_text, txt) == 0) {
gtk_combo_box_set_active(GTK_COMBO_BOX(box), i);
return true;
//break;
}
i++;
valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(list_store), &iter);
}
printf("failed to find the text in the entry list for the combo box\n");
}
如果你在每个组合框线存储更多的信息,您可以得到更多的信息了使用像这样的国际热核实验堆的:
valid = gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter, 0, &item_0, 1, &item_1, 2, &item_2, ... , -1);
希望有所帮助。
你确定最内层的代码运行吗? – 2009-10-31 16:22:32