Treeview

Treeview产生tree或者list,将 GtkTreeModel 接口与 GtkTreeView 小部件结合使用,使用Model/View/Controller模式。

The tree view widget (GtkTreeView)
The view column (GtkTreeViewColumn)
The cell renderers (GtkCellRenderer etc.)
The model interface (GtkTreeModel)

前三个是view。官方文档说:一个model可以使用很多view呈现,例如文件系统,而只需要在内存中存储一种,不知道这个意思是不是使用链接的方式访问同一个文件。

 

1.creat model

model有两种 GtkListStore and the GtkTreeStore

GtkListStore *store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);

一般使用enum去表示:

enum
{
   TITLE_COLUMN = 0,
   AUTHOR_COLUMN,
   CHECKED_COLUMN,
   N_COLUMNS
};

GtkTreeStore *store = gtk_tree_store_new (N_COLUMNS,       /* Total number of columns */
                                          G_TYPE_STRING,   /* Book title              */
                                          G_TYPE_STRING,   /* Author                  */
                                          G_TYPE_BOOLEAN); /* Is checked out?         */

 

建立完model,需要将数据加入。

使用迭代器,gtk_tree_store_append()第三个是父迭代器,可以在其下添加子迭代器。

GtkTreeIter   iter;

gtk_tree_store_append (store, &iter, NULL);  /* Acquire an iterator */

gtk_tree_store_set (store, &iter,
                    TITLE_COLUMN, "The Principle of Reason",
                    AUTHOR_COLUMN, "Martin Heidegger",
                    CHECKED_COLUMN, FALSE,
                    -1);

子迭代器例子:

GtkTreeIter iter1;  /* Parent iter */
GtkTreeIter iter2;  /* Child iter  */

gtk_tree_store_append (store, &iter1, NULL);  /* Acquire a top-level iterator */
gtk_tree_store_set (store, &iter1,
                    TITLE_COLUMN, "The Art of Computer Programming",
                    AUTHOR_COLUMN, "Donald E. Knuth",
                    CHECKED_COLUMN, FALSE,
                    -1);

gtk_tree_store_append (store, &iter2, &iter1);  /* Acquire a child iterator */
gtk_tree_store_set (store, &iter2,
                    TITLE_COLUMN, "Volume 1: Fundamental Algorithms",
                    -1);

gtk_tree_store_append (store, &iter2, &iter1);
gtk_tree_store_set (store, &iter2,
                    TITLE_COLUMN, "Volume 2: Seminumerical Algorithms",
                    -1);

gtk_tree_store_append (store, &iter2, &iter1);
gtk_tree_store_set (store, &iter2,
                    TITLE_COLUMN, "Volume 3: Sorting and Searching",
                    -1);

 

 

2.creat a view

 

GtkWidget *tree;

tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));

 

Cell renderer用于绘制树模型中的数据,包括GtkCellRendererText、GtkCellRendererPixbuf 和 GtkCellRendererToggle。

GtkTreeViewColumn 是 GtkTreeView 用来组织树视图中垂直列的对象。 它需要知道要为用户标记的列的名称、要使用的Cell renderer的类型以及要从模型中检索给定行的哪些数据

GtkCellRenderer *renderer;
GtkTreeViewColumn *column;

renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Author",
                                                   renderer,
                                                   "text", AUTHOR_COLUMN,
                                                   NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);

 一个小例子:

#include <gtk/gtk.h>

enum {
    NAME_COLUMN = 0,
    AGE_COLUMN,
    CHECKED_COLUMN,
    N_COLUMN,
};
void add_data2model(GtkTreeView *tree, const gchar *str ,gint age, gboolean states)
{
    GtkTreeStore *store;
    GtkTreeIter iter;

    store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(tree)));
    gtk_tree_store_append(GTK_TREE_STORE(store), &iter, NULL);
    gtk_tree_store_set(GTK_TREE_STORE(store), &iter, NAME_COLUMN, str, 
                       AGE_COLUMN, age, CHECKED_COLUMN, states);
}

void set_tree(GtkWidget *tree)
{
    GtkTreeStore *store;
    //creat model
    store = gtk_tree_store_new(N_COLUMN, G_TYPE_STRING,G_TYPE_INT, G_TYPE_BOOLEAN);

    gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));


    //creat treeview
    // tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));

    //unreference store and model
    g_object_unref(G_OBJECT(store));

    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;

    //first colum
    renderer = gtk_cell_renderer_text_new();
    g_object_set (G_OBJECT (renderer),
                 "foreground", "red",
                 NULL);

    column = gtk_tree_view_column_new_with_attributes("Name", renderer, "text",
                                                       NAME_COLUMN, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);


    //second colum
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Age",
                                                      renderer,
                                                      "text", AGE_COLUMN,
                                                      NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);

    //last colum
    renderer = gtk_cell_renderer_toggle_new ();
    column = gtk_tree_view_column_new_with_attributes ("Checked out",
                                                      renderer,
                                                      "active", CHECKED_COLUMN,
                                                      NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);

}


int main(int argc, char *argv[])
{

    GtkWidget *window;
    GtkWidget *tree;

    GtkWidget *vbox;
    GtkWidget *label;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    
    gtk_window_set_title(GTK_WINDOW(window), "tree view");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_widget_set_size_request(window, 270, 250);

    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);//gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    tree = gtk_tree_view_new();
    gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), TRUE );
    set_tree(tree);
    gtk_box_pack_start(GTK_BOX(vbox), tree, TRUE, TRUE, 5);

    //add data to model
    add_data2model(tree, "xiaohong", 18, TRUE);
    add_data2model(tree, "WANGMING", 16, FALSE);

    label = gtk_label_new("hello");
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);

    

    g_signal_connect(G_OBJECT (window), "destroy",
                     G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}
View Code

 

posted @ 2021-07-30 11:04  11YS  阅读(174)  评论(0编辑  收藏  举报