views:

35

answers:

2

I've created a window that has some containers and widgets in it, and I want to add a new widget dynamically at run-time to one of the Vboxes in this window. So I have this code, which brings up the window:

gtk_builder_add_from_file( g_builder, "window.xml", NULL );
mainwindow = GTK_WIDGET( gtk_builder_get_object( g_builder, "window" ));
gtk_widget_show( mainwindow );

Then I create a new label, for example, and add it to one of the existing Vboxes, called "vbox_mid", like this:

label = gtk_label_new( "Test label" );
vbox = GTK_WIDGET( gtk_builder_get_object( g_builder, "vbox_mid" ));
gtk_box_pack_end( GTK_BOX( vbox ), label, TRUE, TRUE, 0 );

But this doesn't seem to work. I don't see a new label in the vbox. I have a feeling I'm missing something here, but I can't see what it is. I thought perhaps there was a special GtkBuilder call to add a widget dynamically, but I don't see anything that looks like that. I would really appreciate any help on this.

+1  A: 

Did you remember to show your label after adding it?

Matti Virkkunen
A: 

Matti, thanks! That was the problem. My old code, before I used GtkBuilder, had gtk_widget_show_all in it, while this new code use gtk_widget_show. The old code didn't have any calls to explicitly show the widgets I was adding, so I got confused and thought widgets would automatically be visible.

So the answer seems to be: either call gtk_show_widget on the widgets I'm adding, or make sure I use gtk_show_widget_all on the main window.

Warner Young