曾经做过一段时间网页开发,经常用到微软的treeview控件。创建treeview控件,连接数据库,设置各种属性达到漂亮的效果,均可在清脆的鼠标单击声轻松完成,也是我对treeview最初的印象。然而GTK的treeview控件似乎不那么简单了,即使你用glade完成界面布局,仍然有很多东西需要自己动手去写。国内关于gtk treeview资料相当匮乏,甚至 连GTK+2.0中文教程(网络版)都跳过了treeview的介绍,目前主要学习资料还是gtk-demo里面的三个示例,但是三个示例中解释说明都是一些废话,而且还参杂着其它一些复杂东西,让初学者阅读起来有种腾云驾雾的感觉,造成了不小的心里障碍。本人学习treeview时写过一个超级简单的代码,虽然简单,但五脏俱全,现与大家分享,希望对初学者有所帮助,少走弯路。
1、treeview介绍
Treeview是根据MVC模式进行组织的,它一共可以分为三个部分。模型(Model)部分存储要显示的数据,视图(View)部分控制数据如何显示,而控制器(Controller)部分则控制那些数据用来显示,以及如何对数据进行过滤,排序等等。
为了便于理解,我把使用treeview分成三个步骤:
(1)创建model,用于存储数据。
(2)设置列(column)及其属性。
(3)绑定treeview和model,往treeview添加列。
2、重要的接口
gtk_list_store_new (NUM_COLUMNS, //创建model,注意这个函数的参数,第一个为列数,后面依次是列存储数据的类型
G_TYPE_UINT,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_UINT,
G_TYPE_UINT);
gtk_list_store_append (store, &iter); //把这节点添加到model中
gtk_list_store_set (store, &iter, //为节点添加数据
COLUMN_RANK, data[i].rank,
COLUMN_NAME, data[i].name,
COLUMN_GOLD, data[i].xxxx,
COLUMN_SILVER, data[i].silver,
COLUMN_BRONZE, data[i].bronze,
-1);
gtk_cell_renderer_text_new (); //创建一个文本类型的render
gtk_tree_view_column_new_with_attributes( "1", //创建列并设置其属性
cell_renderer,
"text",
COLUMN_RANK,
NULL);
gtk_tree_view_column_set_sort_column_id (column, COLUMN_RANK); //设置列在treeview的位置。
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column); //将列添加到treeview。
gtk_tree_view_new() //创建一个treeview
gtk_tree_view_set_model //绑定model和tree
3、完整代码
#include <gtk/gtk.h>
#include <glib.h>
typedef struct
{
guint rank;
gchar* name;
guint xxxx;
guint silver;
guint bronze;
}Medal;
enum
{
COLUMN_RANK,
COLUMN_NAME,
COLUMN_GOLD,
COLUMN_SILVER,
COLUMN_BRONZE,
NUM_COLUMNS
};
static Medal data[] =
{
{1, "China", 51, 21, 28},
{2, "United States", 36, 38, 36},
{3, "Russian Fed", 23, 21, 28},
{4, "Great Britain", 19, 13, 15},
{5, "Germany", 16, 10, 15}
};
/*创建model*/
static GtkTreeModel* create_model ()
{
gint i = 0;
GtkListStore* store = NULL;
GtkTreeIter iter = { 0 };
/*创建list store*/
store = gtk_list_store_new (NUM_COLUMNS,
G_TYPE_UINT,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_UINT,
G_TYPE_UINT);
/*添加数据至list store*/
for (i = 0; i < G_N_ELEMENTS (data); i++)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_RANK, data[i].rank,
COLUMN_NAME, data[i].name,
COLUMN_GOLD, data[i].xxxx,
COLUMN_SILVER, data[i].silver,
COLUMN_BRONZE, data[i].bronze,
-1);
}
return GTK_TREE_MODEL (store);
}
/*设置列*/
static void set_columns (GtkTreeView* treeview)
{
GtkCellRenderer* cell_renderer;
GtkTreeViewColumn* column;
/*在treeview中添加一新列,并设置其属性*/
cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "Rank",
cell_renderer,
"text",
COLUMN_RANK,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "NOC Name",
cell_renderer,
"text",
COLUMN_NAME,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "xxxx",
cell_renderer,
"text",
COLUMN_GOLD,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "Silver",
cell_renderer,
"text",
COLUMN_SILVER,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
cell_renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes( "Bronze",
cell_renderer,
"text",
COLUMN_BRONZE,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
return;
}
int main (int argc, char* argv[])
{
GtkWidget* window;
GtkWidget* vbox;
GtkWidget* scrolledwindow;
GtkWidget* treeview;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Files");
vbox = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox);
gtk_container_add (GTK_CONTAINER (window), vbox);
scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow);
gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_SHADOW_IN);
treeview = gtk_tree_view_new ();
gtk_widget_show (treeview);
gtk_container_add (GTK_CONTAINER (scrolledwindow), treeview);
/*设置为FALSE,headers隐藏*/
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE);
GtkTreeModel* model = create_model ();
/*treeview连接model*/
gtk_tree_view_set_model(GTK_TREE_VIEW (treeview), model);
set_columns (GTK_TREE_VIEW (treeview));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 200);
gtk_widget_show (window);
gtk_main ();
return 0;
}