GtkBuilder ignores .ui file

I am trying to create a really simple GUI using GtkBuilder and glade. To achieve this, I follow the official Gtk + 3 Reference Manual. The only difference from the source code is that I donโ€™t connect to widget signals for simplicity (and therefore also removed their callback function):

#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 file "builder.ui" used in the tutorial is as follows:

  <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 does not cause any problems. The program compiles and creates 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 mainloop, and the window does not appear. The GUI file I'm 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?

+4
source share
4 answers

You only read the XML file, you will need code that manipulates the user interface material, for example. 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; } 
+5
source

The difference between your user interface file and the tutorial U file is the line in the GtkWindow widget:

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

Without this, your window (and everything inside it) remains hidden, so nothing happened. The window was there, it was just unnoticed. Line

 gtk_widget_show (window); 

in the solution nos also solves the problem, but thatโ€™s why the tutorial worked and yours didnโ€™t.

+4
source

I had the same problem, but I tried all of the above without success.

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

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

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

Subsequently, I included the path in the compiler, but then again there was no window.

0
source

I had the same problem. A simple application mapping ok on Ubuntu linux did not appear on Raspbian. After some debugging, it looks like there is an error in the GTKBuilder implementation for Raspbian. This does not work.

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

Thanks.

0
source

Source: https://habr.com/ru/post/1395029/


All Articles