Retrieving GTK widget by widget property / ID


I am building a multi-window GTK application. In certain scenarios, the requirement of the application is to change some property of the Widget(s) without user action. One way for that is to iterate through all the children of a GtkWindow and then the child containers to find their children until I get the desired widget. I want to avoid this method as this is not efficient enough.

I am looking for some sort of ID based access of GtkWidgets. Does GTK provides any function to assign ID to the widgets and retrieve the Widgets using the ID later?

The best practice for what you want to achieve is to create composite widget templates using UI description files and derived widget types. Inside instances of your derived types you can keep references to widgets described in your UI definition file, which will then be filled out at construction time, so you have direct access without look up costs.

Thanks @ebassi for the response. Is using GtkBuilder the only way to achieve this? I am not intending to use UI description files. If this is the only way, I have to reconsider my choices.

You can skip the UI definition files, and use derived types with their contents built through the various accessor functions but for maintainability and future proofing you should really consider building complex UIs out of definition files.

In Python we resolve this - when not using templates - by adding class members when creating widgets, so we can manipulate them and their properties in callbacks / when required in code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.