Reputation: 123
What is the correct syntax when using GTK+ 3 (and glade) for creating a simple window from an xml glade file? I have seen two methods that seem to do the same thing however the syntax is slightly different.
Example 1:
#include <gtk/gtk.h>
int
main (int argc, char *argv[])
{
GtkBuilder *builder;
GObject *window;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "builder.glade", NULL);
window = gtk_builder_get_object (builder, "window");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_main ();
return 0;
}
Example 2:
#include <gtk/gtk.h>
void
on_window_destroy (GtkWidget *object, gpointer user_data)
{
gtk_main_quit ();
}
int
main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "builder.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
gtk_builder_connect_signals (builder, NULL);
g_object_unref (G_OBJECT (builder));
gtk_widget_show (window);
gtk_main ();
return 0;
}
Upvotes: 0
Views: 1394
Reputation: 1341
Both should work, but I think it is more reasonable to use GtkWidget
as you will need this type a lot more, so you do not need to write GTK_WIDGET(obj)
all the time. You just need to 'convert' it once.
Please note that the official guide uses GObject
type. (here)
But after all you can decide, as you can easily 'convert' it to the type you need...
Upvotes: 4