Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GtkBuilder ignores .ui file

I am trying to create a really simple GUI using GtkBuilder and glade. To achive this I follow the official Gtk+ 3 Reference manual's tutorial. The only difference to the original code is, that I do not connect to the widget's signals for simplicity (and therefore removed their callback function too):

#include <gtk/gtk.h>

int
main (int   argc,
      char *argv[])
{
  GtkBuilder *builder;
  GObject *window;
  GObject *button;

  gtk_init (&argc, &argv);

  /* Construct a GtkBuilder instance and load our UI description */
  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "builder.ui", NULL);


  gtk_main ();

  return 0;
}

The "builder.ui" file used in the tutorial looks like this:

        <interface>
  <object id="window" class="GtkWindow">
    <property name="visible">True</property>
    <property name="title">Grid</property>
    <property name="border-width">10</property>
    <child>
      <object id="grid" class="GtkGrid">
        <property name="visible">True</property>
        <child>
          <object id="button1" class="GtkButton">
            <property name="visible">True</property>
            <property name="label">Button 1</property>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
        <child>
          <object id="button2" class="GtkButton">
            <property name="visible">True</property>
            <property name="label">Button 2</property>
          </object>
          <packing>
            <property name="left-attach">1</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
        <child>
          <object id="quit" class="GtkButton">
            <property name="visible">True</property>
            <property name="label">Quit</property>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">1</property>
            <property name="width">2</property>
          </packing>
        </child>
      </object>
      <packing>
      </packing>
    </child>
  </object>
</interface>

… and causes no problems at all. The program compiles and produces the desired window. However, when I try to use my own .ui file (generated by glade 3.10.0) I don't get any result at all. The Application enters the mainloop and no window appears. The GUI file I am using is:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="Window">
    <property name="can_focus">False</property>
    <property name="border_width">15</property>
    <child>
      <object class="GtkLabel" id="fooLabel">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">foobar</property>
      </object>
    </child>
  </object>
</interface>

What am I doing wrong?

like image 339
drakide Avatar asked Jan 18 '23 13:01

drakide


1 Answers

You're only reading the xml file, you'll need some code that manipulates the UI stuff , e.g. show the top level window in your .ui :

int main (int   argc, char *argv[])
{
  GtkBuilder      *builder; 
  GtkWidget       *window;

  gtk_init (&argc, &argv);

  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "tutorial.xml", NULL);
  window = GTK_WIDGET (gtk_builder_get_object (builder, "Window"));
  g_object_unref (G_OBJECT (builder));
  gtk_widget_show (window);                
  gtk_main ();

  return 0;
}
like image 199
nos Avatar answered Jan 26 '23 04:01

nos