我有一个简单的GTK程序,可以创建树视图:
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *store;
GtkTreeIter iter;
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);
/*here is some code adding rows using
gtk_list_store_append
gtk_list_store_set*/
return GTK_TREE_MODEL (store);
}
static GtkWidget *
create_view_and_model (void)
{
GtkCellRenderer *renderer;
GtkTreeModel *model;
GtkWidget *view;
view = gtk_tree_view_new ();
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes
(GTK_TREE_VIEW(view),-1,"Name",renderer,"text", COL_NAME,NULL);
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes
(GTK_TREE_VIEW(view),-1,"Age",renderer,"text", COL_AGE,NULL);
model = create_and_fill_model ();
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
g_object_unref (model);
return view;
}
int
main (int argc, char **argv)
{
//usual stuff
view = create_view_and_model ();
gtk_container_add (GTK_CONTAINER (window), view);
//usual stuff
}
有人可以告诉我现在如何更改此树视图的内容?我是否必须再次创建“视图”对象?或者我应该打电话 gtk_tree_view_set_model与新模型?
答案 0 :(得分:1)
要更改GtkTreeView的内容,您必须使用该模型。在你的功能中:
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *store;
GtkTreeIter iter;
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);
/*here is some code adding rows using
gtk_list_store_append
gtk_list_store_set*/
return GTK_TREE_MODEL (store);
}
它评论了你应该追加数据的位置,检索GtkTreeIter
并使用给定的迭代器和函数gtk_list_store_set
设置数据。
您省略了列的枚举器,但从模型GtkListStore创建中可以看出,您将有2列,一列包含字符串,另一列包含无符号整数。 GtkTreeModel是存储(树存储和列表存储)实现的接口。
GtkTreeView遵循MVC(模型视图控制器)模式,因此视图将反映模型中的视图,模型可以具有许多视图。想象一下文件浏览器,其中模型包含有关文件的信息,但您可以拥有多个视图,图标视图或列表视图。
以下是一个示例,与您展示的示例非常相似:
#include <gtk/gtk.h>
enum
{
COL_FIRST_NAME = 0,
COL_AGE,
NUM_COLS
} ;
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *liststore;
GtkTreeIter iter;
liststore = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter,
COL_FIRST_NAME, "Joe",
COL_AGE, 20,
-1);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter,
COL_FIRST_NAME, "Jane",
COL_AGE, 24,
-1);
return GTK_TREE_MODEL(liststore);
}
static GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkTreeModel *model;
view = gtk_tree_view_new();
/* --- Column #1 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "First Name");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* New text renderer */
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
/* Set view column to model column */
gtk_tree_view_column_set_attributes(col, renderer, "text", COL_FIRST_NAME, NULL);
/* --- Column #2 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Age");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* New text renderer */
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
/* Set view column to model column */
gtk_tree_view_column_set_attributes(col, renderer, "text", COL_AGE, NULL);
model = create_and_fill_model();
gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
g_object_unref(model); /* destroy model automatically with view */
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
GTK_SELECTION_NONE);
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
注意我们如何在create_and_fill_model
函数中向模型(GtkListStore)添加数据:
...
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter,
COL_FIRST_NAME, "Joe",
COL_AGE, 20,
-1);
...
还检查我们如何将视图列中的数据绑定到模型列:
gtk_tree_view_column_set_attributes(col, renderer, "text", COL_FIRST_NAME, NULL);
其中col
是第一个包含CellRendererText renderer
的列。
使用(命名文件main.c
)编译给定示例:
gcc -o main main.c `pkg-config --cflags --libs gtk+-3.0`
结果应该是这样的: