4

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?

drakide
  • 1,757
  • 2
  • 15
  • 23

4 Answers4

5

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;
}
nos
  • 223,662
  • 58
  • 417
  • 506
4

The difference between your UI file and the tutorial UI file is this line, in the GtkWindow widget:

<property name="visible">True</property>

Without it, your window (and everything inside it) remains hidden, so that is why "nothing" happened. The window was there, it was just invisible. The line

gtk_widget_show (window);

in nos' solution solves the problem too, but this is why the tutorial file worked and yours didn't.

ptomato
  • 56,175
  • 13
  • 112
  • 165
0

I had the same problem but tried all of the above with no success.

I am used to C++ on Windows, but this is a g++ project on raspbian for a raspberry pi.

My only solution so far was to add the full path as follows

gtk_builder_add_from_file (builder, "/home/pi/Documents/tutorial.xml", NULL);

I subsequently included the path on the compiler but then there was no window once again.

0

I had the same problem. A simple application that displays ok on Ubuntu linux was not appearing in Raspbian. After some debugging it looks like GTKBuilder implementation for Raspbian has a bug. It does not work.

Solution: 1) Either manually convert the GUI description XML to C/C++ API (GTK+) calls. OR 2) Write a small program to convert GUI XML file to C/C++ API (GTK+) calls.

Thanks.