gpt4 book ai didi

c - gtk 处理列表存储 TreeView 中的事件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:21 26 4
gpt4 key购买 nike

我的代码如下

   #include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
GtkWidget *scrolledwindow;

struct param
{
GtkWidget*parent;
GtkWidget*newChild;
};
GtkWidget * init_tree();
GtkWidget * init_tree2();
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}

static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
struct param* parameters = (struct param*)user_data;
gtk_container_remove(GTK_CONTAINER(parameters->parent), (GtkWidget*)tree_view);
gtk_container_add(GTK_CONTAINER(parameters->parent), init_tree2()/*parameters->newChild*/);
}


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, "Mandriva", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Aptosid", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "OpenSuSE", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Linux Mint", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Arch Linux", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Mandriva", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Aptosid", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "OpenSuSE", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Linux Mint", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Arch Linux", -1);
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("Distributions",
cellrenderertext,
"text", 0,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), treeviewcolumn);

return treeview;
}

GtkWidget * init_tree2()
{

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, "a", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "b", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "c", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "d", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "e", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "f", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Aptosid", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "OpenSuSE", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Linux Mint", -1);

gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Arch Linux", -1);

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("Distributions",
cellrenderertext,
"text", 0,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), treeviewcolumn);

return treeview;
}

int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);

GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);

GtkWidget *notebook = gtk_notebook_new();
gtk_container_add(GTK_CONTAINER(window), notebook);


int count;
gchar *text;

for (count = 1; count <= 5; count++)
{
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
text = g_strdup_printf("Page %d", count);
GtkWidget *label = gtk_label_new(text);
GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);

gtk_widget_set_size_request(scrolledwindow, -1, 200);

gtk_container_add(GTK_CONTAINER(vbox), scrolledwindow);
GtkWidget *textview = gtk_text_view_new();
/* gtk_container_add(GTK_CONTAINER(scrolledwindow), textview); */
GtkWidget* treeview = init_tree();
GtkWidget* treeview2 = init_tree2();
struct param* parameters = malloc(sizeof(struct param));
parameters->parent = scrolledwindow;
parameters->newChild= treeview2;
g_signal_connect(treeview, "row-activated", G_CALLBACK(replace_tab),(gpointer)parameters);
gtk_container_add(GTK_CONTAINER(scrolledwindow), treeview);

gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);
}

gtk_widget_show_all(window);

gtk_main();

return 0;
}

我有 2 个列表存储实例 treview1 和 treeview2。显示 1st- TreeView 1。双击任何列表存储项目时,它会生成“行激活”信号。此信号与带有 G_CALLBACK 的函数“replace_tab”相关联。func "replace_tab "采用 4 个参数 - parent Widget、Widget to remove from parent、widget to add to parent 和 gpointer data。如何将参数传递给此函数。或者有没有其他方法可以解决。

最佳答案

代码不好,但这不是问题,因此,要解决双击时不显示第二个 TreeView 而不是第一个 TreeView 的具体问题:

  1. 正如@Gerhardh 所说,您需要将参数作为最后一个参数传递给回调,否则应用程序将在双击一行时出现段错误:g_signal_connect(treeview, "row-activated", G_CALLBACK(replace_tab) , 参数);
  2. 您必须在 replace_tab 函数中显示新的 TreeView :gtk_widget_show_all (GTK_WIDGET(parameters->parent));

您的代码与上述建议:

#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
GtkWidget * init_tree();
GtkWidget * init_tree2();
GtkWidget *scrolledwindow;
struct param
{
GtkWidget*parent;
GtkWidget*newChild;
};

static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}

static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
struct param* parameters = (struct param*)user_data;
gtk_container_remove(GTK_CONTAINER(parameters->parent), (GtkWidget*)tree_view);
gtk_container_add(GTK_CONTAINER(parameters->parent), init_tree2()/*parameters->newChild*/);
gtk_widget_show_all (GTK_WIDGET(parameters->parent));
}


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, "Mandriva", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Aptosid", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "OpenSuSE", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Linux Mint", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Arch Linux", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Mandriva", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Aptosid", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "OpenSuSE", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Linux Mint", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Arch Linux", -1);

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("Distributions",
cellrenderertext,
"text", 0,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), treeviewcolumn);

return treeview;
}

GtkWidget * init_tree2()
{

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, "a", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "b", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "c", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "d", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "e", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "f", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Aptosid", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "OpenSuSE", -1);
gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Linux Mint", -1);

gtk_list_store_append(liststore, &treeiter);
gtk_list_store_set(liststore, &treeiter, 0, "Arch Linux", -1);

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("Distributions",
cellrenderertext,
"text", 0,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), treeviewcolumn);

return treeview;
}

int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);

GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);

GtkWidget *notebook = gtk_notebook_new();
gtk_container_add(GTK_CONTAINER(window), notebook);


int count;
gchar *text;

for (count = 1; count <= 5; count++)
{
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
text = g_strdup_printf("Page %d", count);
GtkWidget *label = gtk_label_new(text);
GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);

gtk_widget_set_size_request(scrolledwindow, -1, 200);

gtk_container_add(GTK_CONTAINER(vbox), scrolledwindow);
GtkWidget* treeview = init_tree();
GtkWidget* treeview2 = init_tree2();
struct param* parameters = malloc(sizeof(struct param));
parameters->parent = scrolledwindow;
parameters->newChild= treeview2;
g_signal_connect(treeview, "row-activated", G_CALLBACK(replace_tab), parameters);
gtk_container_add(GTK_CONTAINER(scrolledwindow), treeview);

gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);
}

gtk_widget_show_all(window);

gtk_main();

return 0;
}

关于c - gtk 处理列表存储 TreeView 中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47387734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com