我正在编写的程序不显示树。该程序输出以下错误:
(gtk:3210):Gtk-CRITICAL **:gtk_grid_attach_next_to:断言'gtk_widget_get_parent(child)== NULL'失败
代码显然在该功能上失败了,我不知道为什么。我设置的树与示例代码相同。唯一的区别是网格,但我想我可以像任何其他小部件一样附加树。有谁知道如何正确地附上它,还是我错过了什么?
int main (int argc, char** argv)
{
// Main window
GtkWidget* window;
GtkWidget* grid;
GtkWidget* box;
// Menubar
GtkWidget* menubar;
GtkWidget* filemenu;
GtkWidget* file;
GtkWidget* quit;
// List
GtkWidget* tree;
GtkTreeStore* store;
GtkTreeIter iter;
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
/*
* Setup the window and the main grid.
*/
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "imgedit");
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
grid = gtk_grid_new ();
/*
* Create the menubar, and all of it's menus.
*/
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
menubar = gtk_menu_bar_new ();
filemenu = gtk_menu_new ();
file = gtk_menu_item_new_with_label ("File");
quit = gtk_menu_item_new_with_label ("Quit");
gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), filemenu);
gtk_menu_shell_append (GTK_MENU_SHELL (filemenu), quit);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), file);
gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, FALSE, 3);
/*
* Setup the list with files.
*/
store = gtk_tree_store_new (3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT);
gtk_tree_store_append (store, &iter, NULL);
gtk_tree_store_set (store, &iter, FILE_NAME, "test name", FILE_OFFSET, 0, FILE_SIZE, 10, -1);
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
g_object_unref (G_OBJECT (store));
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Name", renderer, "text", FILE_NAME, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
/*
* Attach the grid to the main window and connect all signals.
*/
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (grid));
gtk_grid_attach (GTK_GRID (grid), box, 1, 1, 1, 1);
gtk_grid_attach_next_to (GTK_GRID (grid), box, tree, GTK_POS_BOTTOM, 1, 1);
g_signal_connect (quit, "activate", G_CALLBACK (event_quit), GTK_WINDOW (window));
g_signal_connect (window, "delete-event", G_CALLBACK (event_delete), GTK_WINDOW (window));
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
答案 0 :(得分:2)
检查gtk_grid_attach_next_to()
的参数顺序。您告诉它将box
附加到tree
旁边而不是反之,但tree
尚未附加且box
已附加在上一行中。
虽然错误信息乍一看似乎很神秘,但您真的可以从中获取所需的所有信息:对于初学者来说,“断言失败”应该是您的第一个线索,即函数的输入无效。 (许多GTK函数首先断言它们的输入是有效的。)
下一个线索是查看失败的断言:gtk_widget_get_parent (child) == NULL
。换句话说,这是“child
的父窗口小部件是NULL
”或者用简单的英文“child
窗口小部件没有父窗口(因此不在容器内)。 )“断言失败,所以相反的情况必须如此。
查看对gtk_grid_attach_next_to()
的来电,并注意您已将box
作为child
参数传递;因此断言告诉你,“box
不应该已经在容器内,但它仍然在一个容器里面。”实际上,您使用gtk_grid_attach()
将它放在上一行的容器中。