2014-01-30 80 views
0

我正在編寫的程序不顯示樹。該程序輸出以下錯誤:將樹附加到網格

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

回答

1

檢查參數的順序爲gtk_grid_attach_next_to()。您要求它在tree旁邊附加box,而不是反之,但tree尚未附加,而box已附加在上一行中。儘管錯誤信息看起來似乎很模糊,但您確實可以從中獲得所需的所有信息:對於初學者來說,「斷言失敗」應該是您的第一個線索,即函數的輸入無效。 (許多GTK函數通過聲明其輸入有效而開始。)

下一個線索是看看失敗的斷言:gtk_widget_get_parent (child) == NULL。換句話說,這是「的child父窗口部件是NULL」或用簡單的英語「的child小部件沒有父(因此還不是一個容器內)。」這種說法失敗,所以必須相反是真實的。

看看對gtk_grid_attach_next_to()的調用,並注意您已通過參數box;所以斷言告訴你,「box不應該已經在一個容器內,但它仍然在一個容器內。」事實上,你把它放在上一行使用gtk_grid_attach()的容器中。