3
votes

Now that I'm having a window including many buttons which perform similar behavior so it's natural to use same signal handler for them. But however after the signal happens I just can't differ them from each other. Yes, I have the pointer to the object, but I can't tell is it the first button, the second button, or something else.

I supposed The 'Name' attribute (I set it to 'togglebutton1') in glade can be obtained with gtk_widget_get_name function, but I'm wrong, I got 'GtkToggleButton' instead. The xml file says: <object class="GtkToggleButton" id="togglebutton1">.

So is there anyway to make use of the 'id' property, or is there any better solution to identify thoses buttons? thanks a lot.

1

1 Answers

3
votes

The reason you're getting a value back like "GTKToggleButton" is because you have not manually set the name using gtk_widget_set_name. According to the GTK documentation:

Widget names are the type of the widget by default (e.g. "GtkButton") or can be set to an application-specific value with gtk_widget_set_name().

So use gtk_widget_set_name to make customized names for your buttons, and if you need more specific differentiation between objects, use gtk_widget_path to get the full path-name for the object. i.e., It will return the full window hierarchy, allowing you to differentiate between similarly named buttons in separate windows, or allow you to group buttons by their parent windows names, etc.

Edit: You can also use gtk_buildable_get_name in order to get the actual ID value of a widget from a XML UI definition file. So that's another option if you want to avoid the use of gtk_widget_set_name. I'm assuming you're XML UI definition is GTKBuilder compatible.