我在macOS上使用GTK 3。我想遍历GtkListStore。
我有以下代码:
...
GtkListStore *listStore = gtk_list_store_new(N_COLUMNS, // 2
G_TYPE_LONG, // quantity
G_TYPE_STRING); // name
// I stripped some lines but I have some `gtk_list_store_append` and `gtk_list_store_set` elsewhere.
gtk_tree_model_foreach(GTK_TREE_MODEL(listStore), foreach_func, NULL);
gboolean foreach_func(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data) {
gchar *name, *tree_path_str;
glong quantity;
gtk_tree_model_get(model, iter,
QTY_COLUMN, &quantity,
NAME_COLUMN, &name,
-1);
tree_path_str = gtk_tree_path_to_string(path);
g_print("Row %s: %ld %s\n", tree_path_str,
quantity, name);
g_free(tree_path_str);
g_free(name);
return FALSE;
}
我在此行有一个错误:
gtk_tree_model_foreach(GTK_TREE_MODEL(listStore), foreach_func, NULL);
错误是:
gtk_tree_model_foreach: assertion 'GTK_IS_TREE_MODEL (model)' failed
gtk_tree_model_foreach的原型为:
void
gtk_tree_model_foreach (GtkTreeModel *model,
GtkTreeModelForeachFunc func,
gpointer user_data);
因此在我的代码中,收到的model
不是GtkTreeModel
。问题是,我用GtkListStore
将GtkTreeModel
转换为GTK_TREE_MODEL(listStore)
。
我在这里迷路了,我想念什么?
编辑:确定,结果表明此代码有效,错误在其他地方。我在用g_signal_connect调用的函数原型中犯了一个错误。这使我的listStore为NULL,因此强制转换为GtkTreeModel仍在工作,但仍为NULL,这不满意GTK_IS_TREE_MODEL。