How to use gtk_builder and glade

Hello,
I am trying write app using gtk_builder and *.ui file generated with Glade 3.40.

I took aexample from:

paragraph: Building user interfaces

change ui file (prepare it in glade) and source file too but it does not work and I do not understand why. Please could anybohy help me?

output of my app (it only create empty window without any widgets):

after builder
after window

(definice_UI:43601): GLib-GObject-WARNING **: 19:43:16.270: invalid (NULL) pointer instance

(definice_UI:43601): GLib-GObject-CRITICAL **: 19:43:16.270: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
after button

(definice_UI:43601): GLib-GObject-WARNING **: 19:43:16.270: invalid (NULL) pointer instance

(definice_UI:43601): GLib-GObject-CRITICAL **: 19:43:16.270: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

my file rozlozeni.ui

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="window">
    <property name="can-focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkLabel" id="label">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">label</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">neco napis</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="aktualizuj">
            <property name="label" translatable="yes">aktualizuj</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">3</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="ukonci">
            <property name="label" translatable="yes">ukonci</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">4</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

and code of app:

#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <string>
#include <iostream>

using namespace std;

static void aktualizuj (GtkWidget *widget, gpointer   data) {
  cout << "snazime se aktualizovat" << endl;
}

static void quit_cb (GtkWindow *window) {
  gtk_window_close (window);
}

static void activate (GtkApplication *app, gpointer user_data) {
  /* Construct a GtkBuilder instance and load our UI description */
  GtkBuilder *builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "rozlozeni.ui", NULL);

  cout << "after builder" << endl;

  /* Connect signal handlers to the constructed widgets. */
  GObject *window = gtk_builder_get_object (builder, "window");
  gtk_window_set_application (GTK_WINDOW (window), app);

  cout << "after window" << endl;

  GObject *button = gtk_builder_get_object (builder, "aktualizuj");
  g_signal_connect (button, "clicked", G_CALLBACK (aktualizuj), NULL);

  cout << "after button" << endl;

  button = gtk_builder_get_object (builder, "ukonci");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (quit_cb), window);

  gtk_widget_show (GTK_WIDGET (window));

  /* We do not need the builder any more */
  g_object_unref (builder);
}

int main (int   argc,  char *argv[]) {
#ifdef GTK_SRCDIR
  g_chdir (GTK_SRCDIR);
#endif

  GtkApplication *app = gtk_application_new ("pokusna.testovaci.apka", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);

  int status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Glade does not support GTK4. You cannot use Glade to make a UI definition file that GTK4 will load, as there are fundamental differences between what Glade generates and what GTK4 expects.

See the migration guide: Gtk – 4.0: Migrating from GTK 3.x to GTK 4

The “getting started” documentation should be fixed, sorry about that.

Edited: I’ve removed mentions of Glade from the getting started documentation, so it should not be misleading any more.

For GTK4, Glade should be replaced by Cambalache
It is still under development but is already usable, at least for simple projects (and probably beyond that).

used command to glade generated file:
gtk4-builder-tool simplify --3to4 file.ui
and it helps!

And I’ll try Cambalache. Thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.