4

In glade it is possible to set an unique ID to an object. In the code one can obtain a pointer to this object by searching for it's "glade ID" via gtk_builder_get_object().

However for my current use-case I just want to read out this ID from an GObject. What's the API to do so ?

Alex
  • 1,602
  • 20
  • 33

5 Answers5

6

You can't. The builder ID is stored in the builder internally, not in the GObject.

The reason for this is that IDs must be unique per builder, which would be impossible to enforce if you were able to get and set them via some GObject API.

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

You could use gtk_widget_get_name() to identify an object.

Bachsau
  • 1,213
  • 14
  • 21
  • gtk_widget_get_name() will return widget name ('GtkButton'), not widget id. – user3439968 Jul 12 '19 at 14:51
  • @user3439968 It returns whatever has been set in Glade. Meant primarily for styling, it is a way one can set an arbitrary name that can be checked for later, even though it is not the ID. – Bachsau Jul 13 '19 at 15:11
  • You are right. If name not set then return default widget name ('GtkButton'). If set then return what you set. – user3439968 Jul 13 '19 at 20:45
2

It is possible using Gtk.Buildable.get_name(object). This method will return the Glade object id.

This snippet will print all object ids in your Glade XML:

builder = Gtk.Builder()
builder.add_from_file("my-window.glade"))
for obj in builder.get_objects():
    print(Gtk.Buildable.get_name(obj))
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
0

As stated by @ptomato it's seems not possible.

I found that in that line in the documentation:

All the fields in the GObject structure are private to the implementation and should never be accessed directly.

But you can circumvent it because at one point in your code you were refering to it by the id that you typed in (or the code you wrote type in) so you just need to store it at that point. And link it somehow (with a variable or a data structure) to the name of the variable holding the object.

neozero
  • 31
  • 2
0

I just came across a similar need, get_label() should do for what I want

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 11 '23 at 07:45