|
发表于 2006-7-7 23:33:34
|
显示全部楼层
参考代码来自:http://www.osprg.org/modules/new ... ic_id=6&forum=5
/* 关于列表的例子。
* 编译指令为:gcc `pkg-config --cflags --libs gtk+-2.0` -Wall -g -o list list.c
*/
#include <gtk/gtk.h>
int
main (int argc, char *argv[])
{
GtkWidget *main_window;
GtkWidget *tree_view;
GtkListStore *list_store;
GtkTreeViewColumn *column;
GtkCellRenderer *cell_renderer;
GtkTreeIter tree_iter;
gtk_set_locale ();
gtk_init (&argc, &argv);
main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (main_window), 320, 240);
gtk_window_set_title (GTK_WINDOW (main_window), "Main Window");
list_store = gtk_list_store_new (5, G_TYPE_STRING, G_TYPE_UINT,
G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING);
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
gtk_widget_show (tree_view);
gtk_container_add (GTK_CONTAINER (main_window), tree_view);
/* 第一列:每列多个 CelRenderer,每个 CellRenderer 多个属性 */
column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, "0001");
gtk_tree_view_column_set_resizable (column, TRUE);
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_fixed_width (column, 100);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
cell_renderer = gtk_cell_renderer_pixbuf_new ();
gtk_tree_view_column_pack_start (column, cell_renderer, TRUE);
gtk_tree_view_column_set_attributes (column, cell_renderer, "stock-id", 0,
NULL);
/* 一个 CellRenderer 的多个属性与 TreeModel 的多个数据项联系 */
cell_renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, cell_renderer, TRUE);
gtk_tree_view_column_set_attributes (column, cell_renderer, "text", 1,
"foreground", 2, NULL);
/* 第二列 */
column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, "0002");
gtk_tree_view_column_set_resizable (column, TRUE);
/* 下面注释的语句是为了试试 GtkTreeViewColumnSizing
* 大家不妨,把注释去掉,看看结果有何不同
*/
// gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
// gtk_tree_view_column_set_fixed_width (column, 100);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
cell_renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, cell_renderer, TRUE);
gtk_tree_view_column_set_attributes (column, cell_renderer, "text", 3,
NULL);
/* 第三列 */
column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, "0003");
gtk_tree_view_column_set_resizable (column, TRUE);
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_fixed_width (column, 100);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
cell_renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, cell_renderer, TRUE);
gtk_tree_view_column_set_attributes (column, cell_renderer, "text", 4,
NULL);
gtk_list_store_append (list_store, &tree_iter);
gtk_list_store_set (list_store, &tree_iter, 0, "gtk-cdrom", 1, 1000,
2, "red", 3, "string01..............", 4, "string02",
-1);
gtk_list_store_append (list_store, &tree_iter);
gtk_list_store_set (list_store, &tree_iter, 0, "gtk-help", 1, 2000,
2, "green", 3, "string11", 4, "string12", -1);
g_signal_connect (G_OBJECT (main_window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (main_window);
gtk_main ();
return 0;
}
运行效果: |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|