c:GtkListStore行更改事件 - 如何获取所选行的数据?

时间:2011-02-05 09:11:19

标签: c gtk glade gtktreeview

您好我使用glade编写一个小的gtk应用程序。 该应用程序包括一个列表存储。每当用户点击一行时,我希望能够获取该行包含的信息。

在行改变事件下使用glade我添加了以下功能:

void gtk_tree_view_row_activated_event_handler(GtkWidget *widget, gpointer data) {

    GtkTreeSelection *selection;
    GList *rows_list;

    selection = gtk_tree_view_get_selection(widget);
    rows_list = gtk_tree_selection_get_selected_rows (selection, widget);       
}

我如何从这里继续?

1 个答案:

答案 0 :(得分:3)

请检查以下代码是否适合您:

enum
{
    LIST_ITEM = 0,
    N_COLUMNS
};

void view_selected_foreach_func(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer userdata)
{
    char *value;
    gtk_tree_model_get(model, iter, LIST_ITEM, &value, -1);
    g_print("%s is selected\n", value);
    g_free(value);
}

void gtk_tree_view_row_activated_event_handler(GtkWidget *widget, gpointer data)
{
    GtkTreeSelection *selection;
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
    gtk_tree_selection_selected_foreach(selection, view_selected_foreach_func, NULL);
}

我想另一种实现你想要做的事情的方法是为GtkTreeSelection changed信号定义一个处理程序。以下是一个小例子:

设置树视图:

list = gtk_tree_view_new();
...
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
g_signal_connect(selection, "changed", G_CALLBACK(on_changed), label);

更改了信号处理程序:

void on_changed(GtkWidget *widget, gpointer label)
{
    GtkTreeIter iter;
    GtkTreeModel *model;
    char *value;

    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
    {
        gtk_tree_model_get(model, &iter, LIST_ITEM, &value,  -1);
        g_print("%s is selected\n", value);
        g_free(value);
    }
}

希望这有帮助,尊重