gtk-get在liststore treeview

时间:2017-11-21 18:52:30

标签: c linux list treeview gtk

我想获取在树视图中单击的项目,这会导致“行激活”事件。我在树视图中有一个列表存储。单击其中的任何项目将显示另一个列表。但是我怎么知道点击了哪个项目? 我怎么找到的? 代码 -

GtkWidget * init_tree()
{

    GtkListStore *liststore = gtk_list_store_new(1, G_TYPE_STRING);
    GtkTreeIter treeiter;

    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Register", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "New Configuration", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Edit Configuration", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Delete Configuration", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Add location to turn-off place list", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Disable Turn-off Places", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Enable Turn-off Places", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Disable Distance", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Enable Distance", -1);
    gtk_list_store_append(liststore, &treeiter);
    gtk_list_store_set(liststore, &treeiter, 0, "Factory Reset", -1);
    gtk_list_store_append(liststore, &treeiter);
    GtkWidget *treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liststore));

    GtkCellRenderer *cellrenderertext = gtk_cell_renderer_text_new();

    GtkTreeViewColumn *treeviewcolumn = gtk_tree_view_column_new_with_attributes("Settings",
                                                                                 cellrenderertext,
                                                                                 "text", 0,
                                                                                 NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), treeviewcolumn);

    return treeview;
}

static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
..........
}

int main(...)
{
....
g_signal_connect(treeview, "row-activated", G_CALLBACK(replace_tab), (gpointer) parameters);

.....
}

1 个答案:

答案 0 :(得分:3)

双击(treeview)时获取row-activated选项的最常用方法是:

<强> 1。使用row-activated回调

中的变量

来自row-activated信号的信号处理程序是:

void
user_function (GtkTreeView       *tree_view,
               GtkTreePath       *path,
               GtkTreeViewColumn *column,
               gpointer           user_data)

我们可以使用公开的变量从模型中检索数据;从GtkTreePath path我们得到GtkTreeIter iter,从GtkTreeView treeview我们得到GtkTreeModel model。拥有modeliter,我们可以从model检索数据;例如。在replace_tab信号处理程序中,添加以下内容:

static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
   gchar *string;
   GtkTreeIter iter;
   GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
   gtk_tree_model_get_iter(model, &iter, path); 
   gtk_tree_model_get (model, &iter, 0, &string, -1);
   g_print ("activated: %s\n", string);
   ...

 }

这应该从激活的行

打印文本

<强> 2。使用GtkTreeSelection

另一种选择是使用我们可以从GtkTreeSelection本身检索的GtkTreeView。此步骤允许我们提取选定的iter(树视图上的单个选择),我们将从model获取数据;例如,在replace_tab信号处理程序中,添加以下内容:

static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{ 
   gchar *string;
   GtkTreeIter iter;
   GtkTreeSelection *selection;
   GtkTreeModel *model = gtk_tree_view_get_model (tree_view);

   selection = gtk_tree_view_get_selection (tree_view);
   gtk_tree_selection_get_selected (selection, &model, &iter); 
   gtk_tree_model_get (model, &iter, 0, &string, -1);
   g_print ("activated: %s\n", string);
   ...
}

<强>结论:

选项1)它更容易,更清洁,更直接。通常2)它用于通过changed发出的GtkTreeSelection信号进行选择更改。