如何获得 GtkTreePath 或 GtkTreeIter 到 GtkTreeModel 中的最后一行?

fre*_*sch 5 c python gtk pygtk

我想在 GtkTreeModel 的最后一行获取 GtkTreePath 或 GtkTreeIter,但 GtkTreeModel 没有相应的功能。

我会对 C 或 Python 或两者中的答案和示例感到满意;)。

unw*_*ind 4

你应该能够通过 GtkTreePath,可能有点 hack,但仍然在 API 内:

GtkTreePath *path;
GtkTreeIter iter;

/* With NULL as iter, we get the number of toplevel nodes. */
gint rows = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(model), NULL);

/* Now get a path from the index. */
path = gtk_tree_path_new_from_indices(rows - 1, -1);

/* Ask the model for an iter to the node identified by the path. */
gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path);

/* Drop the path, we're done with it, iter is final output. */
gtk_tree_path_free(path);
Run Code Online (Sandbox Code Playgroud)

注意:我在编写时还没有测试过上面的内容,但我很确定我在实际代码中做了一些非常相似的事情。