Home GTK [转]GTK+学习笔记-树型视图与模型(GtkTreeView)

GtkWidget *mytree = NULL;
GtkTreeStore *lefttree = NULL; 

树型模型的建立:

void setupTreeStore()
{
    lefttree = gtk_tree_store_new(1,G_TYPE_STRING);    
    GtkTreeIter   iter ;
    GtkTreeIter   child;
    gtk_tree_store_append (lefttree, &iter, NULL);//建立一个空行
    gtk_tree_store_set (lefttree, &iter ,0,"root1",-1);//为此行设置值

    gtk_tree_store_append (lefttree, &iter, NULL);//建立另一个空行
    gtk_tree_store_set (lefttree, &iter ,0,"root2",-1);//为此行设置值
    
    gtk_tree_store_append (lefttree, &child, &iter);//为上一行建立一个子行
    gtk_tree_store_set (lefttree, &child ,0,"child1",-1);//为此子行设置值
    

//更多行依此类推..........

}



树型视图的建立以及与数据模型的关联:

//要想让视图显示出数据,必须建立GtkCellRenderer与GtkTreeViewColumn
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;

//关联视图与模型
gtk_tree_view_set_model(GTK_TREE_VIEW(mytree),GTK_TREE_MODEL(lefttree));

//将数据模型交给视图管理,视图销毁时数据会被一同销毁
g_object_unref (G_OBJECT (lefttree));

//建立一个GtkCellRenderer 
renderer = gtk_cell_renderer_text_new ();

//设置GtkCellRenderer的渲染方式
g_object_set (G_OBJECT (renderer),
                 "foreground", "red",
                 NULL);

//建立一个带标题的列 并且将renderer放入其中使其能显示内容
column = gtk_tree_view_column_new_with_attributes ("Family", renderer,
                                                                                               "text", 0,
                                                                                               NULL);
//将列设置为可以手动改变大小
 gtk_tree_view_column_set_resizable(column,TRUE);

//将列加入gtk_tree_view
gtk_tree_view_append_column (GTK_TREE_VIEW (mytree), column);
    

gtk_widget_show_all(mytree);

下面为建立好的树型视图实现一个功能:
      当用户选中一个有子行的行时按Enter或Space键展开或收起子行,当选中一个没有子行的行时按Enter或Space键后台打印一些信息。

/*
TreeView的"row-activated"信号产生条件:
    --在gtk_tree_view_row_activated()被调用时
    -- 一个行被双击时
    -- 选中一个不可编辑的行,按下Space, Shift+Space, Return,Enter键时
*/
//此回调函数与 TreeView的"row-activated"信号相连接
void on_lefttree_row_activated  (GtkTreeView       *tree_view,
                                 GtkTreePath       *path,
                                 GtkTreeViewColumn *column,
                                 gpointer           user_data)    
...{
    GtkTreeIter   iter ;
    //根据path获得iter
     if (gtk_tree_model_get_iter(GTK_TREE_MODEL(lefttree), &iter, path))
    ...{
        //得到iter,判断iter指向的行是否有子行
        if ( gtk_tree_model_iter_has_child (GTK_TREE_MODEL(lefttree), &iter) )
        ...{
            //有子行,展开本行
            if ( gtk_tree_view_expand_row(tree_view,path,FALSE) )
            ...{
                //展开本行成功
                g_print("expand child ");
            }
            else //本行已经被展开
            ...{
                //收起本行
                gtk_tree_view_collapse_row(tree_view,path);
                g_print("collapse child ");
            }
        }
        else
        ...{
            //没有子行,直接执行规定的功能
            g_print("do some ");
        }
    }

    

}



获得treeview中选中的一行的GtkTreeIter或path:

GtkTreeSelection* select = gtk_tree_view_get_selection( tree_view );
gtk_tree_selection_select_path( select, path );
gtk_tree_selection_select_iter( select, &iter ); 



要想更改GtkTreeIter指向的数据的值,使用:

gtk_tree_store_set  (lefttree, &iter,CHECKED_COLUMN ,data);


打赏
0 comment

You may also like

Leave a Comment

*

code