3

I'm writing a Gtk+3 application in C.

The user interface is built with glade.

I created an independent GtkBox that should be used in several places in my application. When I try to use that GtkBox more then once I get the following error:

(tuxmusicstudio:27491): Gtk-WARNING **: Attempting to add a widget with type GtkBox to a container of type GtkBox, but the widget is already inside a container of type GtkBox, please use gtk_widget_reparent()

How can I clone the GtkWidget so it will be reusable again and again?

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
ufk
  • 30,912
  • 70
  • 235
  • 386

1 Answers1

3

You can't clone a widget as such. Either put your reusable widget into its own Glade file and load it more than once using GtkBuilder; or create a class for the widget you want and instantiate it more than once.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 1
    do I really need a new instance of GtkBuilder whenever I want to reuse the widget ? – ufk Mar 23 '14 at 09:05
  • Yes. when you load a Glade file with GtkBuilder, it creates exactly one instance of each of the widgets described in the Glade file. However, if you create a class that uses [`gtk_widget_class_set_template()`](https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-class-set-template) and friends, that will be more work up front but all the GtkBuilder stuff will be taken care of. – ptomato Mar 23 '14 at 18:49