Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

c - GtkTreeView set selection to specific row

How can I set the GtkTreeSelection to a specific row, to the row number 3?

I can set the selection to the GtkTreeIter, but how can i set the iter to the row number 3?

I didn't find anything useful at the google search, so I didn't try anything yet because I don't know what.

I hope you can help me and give me information about my questions!

EDIT:

GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
GtkTreePath *path = gtk_tree_path_new_from_indices(3, -1);
gtk_tree_model_get_iter(model, &iter, path);
gtk_tree_path_free(path);
gtk_tree_selection_select_path(treeview_selection, path);

-> Don't work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to use a GtkTreeIter for this, the GtkTreePath API is enough. You're throwing your path away before using it, which creates problems.

Here's how to do it:

GtkTreePath *path = gtk_tree_path_new_from_indices(3, -1);
gtk_tree_selection_select_path(treeview_selection, path);
gtk_tree_path_free(path);

UPDATE: I rewrote the code completely to drop use of GtkTreeIter, I originally thought that you wanted a solution using an iter since that was what you were trying to do.

If you just want to do a selection (and don't, for instance, need a GtKTreeIter for something else) the above is the simplest way using just a GtkTreePath.

Take care not do destroy the path before using it in the select-call, of course.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...