GtkBuilder and "names"
Asked Answered
M

2

6

I've been trying to get the "names" of all GtkWidgets in a GtkBuilder object.

I've managed to get all objects from the builder object via gtk_builder_get_objects() and store them in a GSList.

However, when I use gtk_widget_get_name() on the gobjects (which i cast to GtkWidgets), I get generic names such as "GtkWindow" and "GtkButton" instead of "window1" or "button1" that are displayed in glade.

Any help would be extremely appreciated and would make this programmer very happy.

Mcclintock answered 25/11, 2011 at 0:29 Comment(1)
Here's the same question for Python with GObject introspection: https://mcmap.net/q/1774474/-how-can-i-retrieve-from-a-gtk-builder-the-name-of-an-object-duplicate/247696Lunseth
L
4

The names set for the builder are not the same as name of the GtkWidget. GtkBuilder maintains an internal hash table which has the name set in GtkBuilder UI definitions from the file or string (from which builder was added) and associated object. It is used to retrieve the objects when gtk_builder_get_object () is called. Make use of the "name" property of GtkWidget. Set "name" property in the GtkBuilder UI definitions to set the name of the GtkWidget which can be retrieved using gtk_widget_get_name().
Hope this helps!

Loria answered 25/11, 2011 at 3:19 Comment(2)
Thank you so much. I mean this, THANK YOU. Off-topic: The gtkbuilder page didn't say or emphasize on the fact that there are two separate "name" properties. Do you know where I can get better information regarding glade and gtkbuilder?Mcclintock
@seininn: You are welcome! Well GTkBuilder page never says that name is the set is the property of the associated GtkWidget. It says that its the name assigned to them in UI description. So the property "name" associated with GtkWidget is not the one being set but it an identifier. I think you got it mixed up :) ... I think online documentations & forums are the best resources I can think of for glade & GtkBuilderLoria
E
5

I am doing the exact same thing. I was able to get the id= strings from the .glade file using gtk_buildable_get_name() as specified here.

Note: Prior to 2.20, GtkBuilder was setting the "name" property of constructed widgets to the "id" attribute. In GTK+ 2.20 or newer, you have to use gtk_buildable_get_name() instead of gtk_widget_get_name() to obtain the "id", or set the "name" property in your UI definition.

Epiphysis answered 29/2, 2012 at 18:17 Comment(1)
Note to those using Perl: $widget->get_name() calls gtk_widget_get_name(). To call gtk_buildable_get_name(), use: $name = Gtk3::Buildable::get_name($widget)Andri
L
4

The names set for the builder are not the same as name of the GtkWidget. GtkBuilder maintains an internal hash table which has the name set in GtkBuilder UI definitions from the file or string (from which builder was added) and associated object. It is used to retrieve the objects when gtk_builder_get_object () is called. Make use of the "name" property of GtkWidget. Set "name" property in the GtkBuilder UI definitions to set the name of the GtkWidget which can be retrieved using gtk_widget_get_name().
Hope this helps!

Loria answered 25/11, 2011 at 3:19 Comment(2)
Thank you so much. I mean this, THANK YOU. Off-topic: The gtkbuilder page didn't say or emphasize on the fact that there are two separate "name" properties. Do you know where I can get better information regarding glade and gtkbuilder?Mcclintock
@seininn: You are welcome! Well GTkBuilder page never says that name is the set is the property of the associated GtkWidget. It says that its the name assigned to them in UI description. So the property "name" associated with GtkWidget is not the one being set but it an identifier. I think you got it mixed up :) ... I think online documentations & forums are the best resources I can think of for glade & GtkBuilderLoria

© 2022 - 2024 — McMap. All rights reserved.