More menus

As we saw in the last post, I was struggling to use the newer style GtkBuilder methods to start replacing bits of the application kludge3d. But my simple minded approach was giving me some grief:

 #include <gtk/gtk>
int main(int argc, char **argv)
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    GtkBuilder* builder = gtk_builder_new_from_file ("test.ui");
    GtkWidget* menubar = GTK_WIDGET(gtk_builder_get_object (builder, "menubar"));
    gtk_container_remove(GTK_CONTAINER(gtk_builder_get_object (builder, "mainvbox")), menubar);
    GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_box_pack_start(GTK_BOX(vbox), menubar, TRUE, TRUE, 2);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    return 0;

This isn’t so promising:
and the runtime warning isn’t clear (though as we’ll see, it’s correct)

(a.out:5940): Gtk-CRITICAL **: gtk_box_pack: assertion ‘gtk_widget_get_parent (child) == NULL’ failed

ToGoogle! Ok, consensus is that my menubar widget already has a parent and so the mainvbox is complaining when I try to add the menubar.

Memories of making my own container widgets in Windows and Gtk+2.x, I need to reparent my menubar.


gtk_widget_reparent has been deprecated since version 3.14 and should not be used in newly-written code.

Use gtk_container_remove() and gtk_container_add().

Great, at least I know to add :

gtk_container_remove(GTK_CONTAINER(gtk_builder_get_object (builder, "mainvbox")), menubar);

And, it works: