我正在編寫的程序不顯示樹。該程序輸出以下錯誤:將樹附加到網格
(gtk:3210): Gtk-CRITICAL **: gtk_grid_attach_next_to: assertion 'gtk_widget_get_parent (child) == NULL' failed
該代碼顯然在該功能失敗,我不知道爲什麼。我設置的樹與示例代碼相同。唯一的區別是網格,但我認爲我可以像其他任何小部件一樣附加樹。有誰知道如何正確附加它,或者我錯過了什麼?
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;
}