How to access multiple GTK+ widgets using Glib and the GObject

See my former post on “How to access two GTK+ widgets from a single callback function (using libglade)” for a different way of achieving the same thing. However, the method described below is the official way of doing things and thus to be preferred.

GtkWidgets are derived from GObjects. They can be cast back and forth using the G_OBJECT() and GTK_WIDGET() macros. Use g_object_set_data() to attach a gpointer and its key to a GObject of your choice (find the definition at library.gnome.org). The pointers can then be retrieved wherever the GObject that they have been attached to, is available. This is done using g_object_get_data(). Attaching two pointers to two different GtkWidgets to the main window widget, will enable you to call on them from any callback function you use.

Leave a Comment.