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

发布于 2024-10-16 03:39:06 字数 425 浏览 2 评论 0原文

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

在行更改事件下使用空地,我添加了以下功能:

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

如何从这里继续?

Hello I use glade to write a small gtk application.
the application includes a list store. and whenever a user clicks on a row, i want to be able to fetch the information that this row contains.

using glade under row-changed event i added the following function:

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

how do i continue from here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2024-10-23 03:39:06

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

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 已更改 信号。下面是一个小例子:

设置树视图:

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

希望这有帮助,问候

pls, check if the code below would work for you:

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

I guess another way to achieve what you're trying to do would be defining a handler to the GtkTreeSelection changed signal. Below is a small example:

setting up tree view:

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

changed signal handler:

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

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文