本文介绍了gtk-get在liststore树视图中点击哪个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到在treeview中引起行激活事件的点击项目。我有一个树形视图中的列表商店。点击它的任何项目都会显示另一个列表。但是,我怎么知道哪个项目被点击?
我如何找到?
code -

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 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,新配置,-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,将位置添加到关闭位置列表,-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,启用关闭位置,-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,启用距离,-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);

返回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)参数);

.....
}


解决方案

通过双击获得 treeview 选项的最常用方法(行激活)是:

1。使用来自行激活的回调函数的变量

来自行激活信号是:

我们可以从暴露变量的模型中检索数据;从> GtkTreePath路径,我们得到 GtkTreeIter iter 并从> GtkTreeView treeview 我们可以得到 GtkTreeModel模型。拥有模型和 iter ,我们可以从模型中检索数据;例如。在您的 replace_tab 信号处理程序中,添加以下内容:

  static void replace_tab (GtkTreeView * tree_view,GtkTreePath *路径,GtkTreeViewColumn *列,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\\\
,string);
...

}

这会打印文本来自激活的行

2。使用GtkTreeSelection



另一种选择是使用 GtkTreeSelection ,我们可以从 GtkTreeView 本身。通过这一步,我们可以从模型中获取选定的 iter (树形视图中的单个选择);例如在你的 replace_tab 信号处理程序中添加以下内容:

  static void replace_tab(GtkTreeView * tree_view,GtkTreePath *路径,GtkTreeViewColumn *列,gpointer user_data)
{
gchar * string;
GtkTreeIter iter;
GtkTreeSelection *选择;
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\\\
,string);
...
}

结论:

选项1)更简单,更简洁,更直接。通常2)通过 GtkTreeSelection 发出的改变信号来获得选择更改。


I want to get the item that is clicked in the treeview which causes "row-activated" event. I have a list store in a treeview. Clicking any item of it shows another list. But how do I know which item is clicked?How do I find that?code-

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);

.....
}
解决方案

The most common ways to get the treeview selection on double click (row-activated) are:

1. Using variables from the row-activated callback

The signal handler from row-activated signal is:

We can retrieve data from the model with the exposed variables; from the GtkTreePath path we get the GtkTreeIter iter and from the GtkTreeView treeview we can get the GtkTreeModel model. Having the model and the iter we can retrieve data from the model; eg. in your replace_tab signal handler, add the following:

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);
   ...

 }

This should print the text from the activated row

2. Using GtkTreeSelection

Another option is to use GtkTreeSelection which we can retrieve from the GtkTreeView itself. This step allow us to extract the selected iter (single selection on the tree view) with which we will get the data from the model; eg in your replace_tab signal handler, add the following:

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);
   ...
}

Conclusion:

Option 1) it's easier, cleaner and straight forward. Normally 2) it's used to get selection changes via the changed signal emitted by GtkTreeSelection.

这篇关于gtk-get在liststore树视图中点击哪个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 08:06