Home > Software design >  Load and use a Glade layout in GTK2/C
Load and use a Glade layout in GTK2/C

Time:05-09

I was trying to load a Glade layout into a GTK2 C code.

It consists of a window in which there is a VBox with 2 elements: a label and a button. I want to associate a signal to the button to close the application.

If I make all of this by using only C code it works fine, but when I load a layout I receive the following runtime error (the application starts but the button doesn't work):

(main:2581): GLib-GObject-WARNING **: 21:38:46.789: invalid (NULL) pointer instance

(main:2581): GLib-GObject-CRITICAL **: 21:38:46.790: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

Here the code I used, mylayout.glade is a file in my project folder:

#include <gtk/gtk.h>

void
end_program (GtkWidget * wid, gpointer ptr)
{
    gtk_main_quit();
    
    return;
}

int
main (int argc, char * argv[])
{
    GError * error = NULL;
    
    gtk_init(&argc, &argv);
    
    GtkBuilder * builder = gtk_builder_new();
    
    if (0 == gtk_builder_add_from_file(builder, "mylayout.glade",
                                       &error))
    {
      g_printerr("Error loading file: %s\n", error->message);
      g_clear_error(&error);
      
      return 1;
    }

    GtkWidget * win = (GtkWidget *) gtk_builder_get_object(builder,
                                                           "window1");
    GtkWidget * btn = (GtkWidget *) gtk_builder_get_object(builder,
                                                           "button1");
    
    g_signal_connect(btn, "clicked", G_CALLBACK(end_program), NULL);
                                                           
    gtk_widget_show_all(win);
    gtk_main();
    
    return 0;
}

The only thing I noticed is that if I remove the g_signal_connect() line the error disappear.

Any help is really appreciated.

CodePudding user response:

You might want to check the name you gave your button widget in the glade file. The only way I could replicate your error was when the name of the button widget defined in the glade file was something different from "button1" as referenced in the program statement:

GtkWidget * btn = (GtkWidget *) gtk_builder_get_object(builder, "button1");

When the glade file had a different name for the button (e.g. "button" or "btn"), then I received the identical warning and critical error message as you noted.

Regards.

  • Related