gpt4 book ai didi

c - gtk-back 按钮不会从网格中删除,列表存储 TreeView 行未显示完整

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

我有两个问题:

  1. 列表存储项目未完整显示,已被截断。

如何显示完整列表?

#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
GtkWidget *scrolledwindow;
struct param
{
GtkWidget* grid;
GtkWidget* scrolledwindow;
GtkWidget*newChild;
GtkWidget*oldChild;
};
GtkWidget * init_tree();
GtkWidget * init_tree2();
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}

static void Back (GtkButton *button, gpointer user_data)
{
struct param* parameters = (struct param*)user_data;
gtk_container_remove(GTK_CONTAINER(parameters->scrolledwindow), parameters->oldChild);
gtk_container_add(GTK_CONTAINER(parameters->scrolledwindow), parameters->newChild);
gtk_grid_remove_row (GTK_GRID (parameters->grid),20);
gtk_widget_show_all (GTK_WIDGET(parameters->scrolledwindow));
gtk_widget_show_all (GTK_WIDGET(parameters->grid));


}


static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
struct param* parameters = (struct param*)user_data;
//gtk_grid_insert_row ( GTK_GRID(grid),2);
gtk_container_remove(GTK_CONTAINER(parameters->scrolledwindow), GTK_WIDGET(tree_view));
gtk_container_add(GTK_CONTAINER(parameters->scrolledwindow), parameters->newChild);
GtkWidget *button = gtk_button_new_with_label ("Back");
gtk_grid_attach (GTK_GRID (parameters->grid), button, 0, 20, 2, 2);
parameters->oldChild = parameters->newChild;
parameters->newChild= init_tree();
g_signal_connect (button, "clicked", G_CALLBACK (Back), parameters);
gtk_widget_show_all (GTK_WIDGET(parameters->scrolledwindow));
gtk_widget_show_all (GTK_WIDGET(parameters->grid));
}

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* grid = gtk_grid_new ();
GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);

gtk_widget_set_size_request(scrolledwindow, -1, 200);

gtk_container_add(GTK_CONTAINER(vbox), grid/*scrolledwindow*/);

//gtk_container_add(GTK_CONTAINER(grid), scrolledwindow);
gtk_grid_attach (GTK_GRID (grid), scrolledwindow, 0, 0, 200, 10);
//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->scrolledwindow = scrolledwindow;
parameters->grid = grid;
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;
}

最佳答案

首先,代码不好,但这不是问题,因此,要解决在显示第二个treeview 时实现后退按钮的具体问题在同一笔记本页面中,您可以重复使用包含 treeviewvbox 并在其下方添加一个按钮。您必须将 no-show-all 属性设置为 true 并自行设置显示/隐藏状态。基本上,您只想在显示第二个 treeview 时显示它。

我使用了 GtkBox 打包函数并在回调中显示和隐藏后退按钮。

通过这些小的调整来尝试您的代码:

#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>
GtkWidget *scrolledwindow;
struct param
{
GtkWidget* backbutton;
GtkWidget* scrolledwindow;
GtkWidget*newChild;
GtkWidget*oldChild;
};
GtkWidget * init_tree();
GtkWidget * init_tree2();
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}

static void Back (GtkButton *button, gpointer user_data)
{
struct param* parameters = (struct param*)user_data;
gtk_widget_hide(GTK_WIDGET(parameters->scrolledwindow));
gtk_container_remove(GTK_CONTAINER(parameters->scrolledwindow), GTK_WIDGET(parameters->newChild));
gtk_container_add(GTK_CONTAINER(parameters->scrolledwindow), parameters->oldChild);
gtk_widget_hide (GTK_WIDGET(parameters->backbutton));
gtk_widget_show_all (GTK_WIDGET(parameters->scrolledwindow));
}


static void replace_tab(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
{
struct param* parameters = (struct param*)user_data;
gtk_widget_hide(GTK_WIDGET(parameters->scrolledwindow));
gtk_container_remove(GTK_CONTAINER(parameters->scrolledwindow), GTK_WIDGET(tree_view));
gtk_container_add(GTK_CONTAINER(parameters->scrolledwindow), parameters->newChild);
gtk_widget_show (GTK_WIDGET(parameters->backbutton));
gtk_widget_show_all (GTK_WIDGET(parameters->scrolledwindow));
}

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++)
{
text = g_strdup_printf("Page %d", count);
GtkWidget *label = gtk_label_new(text);

GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);

GtkWidget *backbutton = gtk_button_new_with_label ("Back");
gtk_widget_set_no_show_all (backbutton, TRUE);

gtk_box_pack_start (GTK_BOX(vbox), scrolledwindow, TRUE, TRUE, 0);
gtk_box_pack_end (GTK_BOX(vbox), backbutton, FALSE, FALSE, 0);


GtkWidget* treeview = init_tree();
GtkWidget* treeview2 = init_tree2();
struct param* parameters = malloc(sizeof(struct param));
parameters->backbutton = backbutton;
parameters->scrolledwindow = scrolledwindow;
parameters->oldChild= treeview;
parameters->newChild= treeview2;

g_signal_connect(treeview, "row-activated", G_CALLBACK(replace_tab), (gpointer) parameters);
g_signal_connect(backbutton, "clicked", G_CALLBACK(Back), (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;
}

您的 Widget 包装不好。我建议你试试 Glade并将其用作学习小部件放置的 Playground 。

同时了解 GtkStack这将更适合这个目的,甚至可以添加“酷”动画过渡。

关于c - gtk-back 按钮不会从网格中删除,列表存储 TreeView 行未显示完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399178/

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