cgtkgladegtkbuilder

C GTK3 program using glade not showing main window


I have created a UI with glade.

When I run the program, it is not giving any errors, but it is not showing the main window either. It just exits.

My full UI file is at https://paste.gnome.org/pfxz888er The full C program is at https://paste.gnome.org/p7rxhhbk2

Please tell me if there is something wrong with my code. Significant parts are:

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>

static void create (GtkApplication *app, gpointer user_data)
{
GtkBuilder *main_interface;
main_interface = gtk_builder_new_from_file ("/home/aj/learning/front.glade");

//  gtk_builder_connect_signals (main_interface,NULL);
gtk_builder_add_callback_symbol (main_interface,
                             "cb_about",
                             G_CALLBACK(cb_about));

gtk_builder_add_callback_symbol (main_interface,
                             "cb_enc_mode",
                             G_CALLBACK(cb_enc_mode));
..... all other callbacks........

GObject *top_window;
top_window = gtk_builder_get_object (main_interface,
                    "top_window");
gtk_widget_show_all (GTK_WIDGET(top_window));
}

int main (int argc, char *argv[])
{
int status;
GtkApplication *app;
app = gtk_application_new ("org.aj.gui",G_APPLICATION_FLAGS_NONE);
g_signal_connect(app,"activate",G_CALLBACK(create),NULL);
status = g_application_run (G_APPLICATION(app),argc,argv);
g_object_unref (app);
return status;
}

Solution

  • Found the issue. I should set the window for the GtkApplication. Additional code needed:

    GtkWidget *top_window;
    top_window = GTK_WIDGET (gtk_builder_get_object (main_interface,"top_window"));
    if (!top_window) {
        g_critical ("Widget \"%s\" is missing in UI file\n",
                "top_window");
    }
    g_object_unref (main_interface);
    gtk_window_set_application (GTK_WINDOW (top_window), GTK_APPLICATION (app));
    gtk_widget_show_all (GTK_WIDGET(top_window));