How to reference an object using gtk builder compiled with glib_build_tools in rust? (original) (raw)
March 15, 2025, 11:40am 1
Hello, good morning.
Im building an application using gtk_rs, and its pretty simple use the gtk builder
to load a xml to describe interface, however, when using gtk_build_tools
, the things gets a bit more complicated.
Im loading my resources using gio::resources_register_include!("resources.gresource").expect("Failed to register resources");
, but I don’t know how to reference the xml objects as we do when loading with include_str!
macro. I had read the gtk_rs book also. How I can access the objects in xml using gtk build tools?
Best Regards
shartrec (Trevor Campbell) March 16, 2025, 5:24am 2
I am loading my resources just as you are.
To reference the xml objects, I do the following in the imp of the window (or template object)
mod imp {
use.....
#[derive(Default, CompositeTemplate)]
#[template(resource = "/com/shartrec/kelpie_planner/airport_view.ui")]
pub struct AirportView {
#[template_child]
pub airport_window: TemplateChild<ScrolledWindow>,
#[template_child]
pub airport_list: TemplateChild<ColumnView>,
#[template_child]
pub col_id: TemplateChild<ColumnViewColumn>,
#[template_child]
pub col_name: TemplateChild<ColumnViewColumn>,
#[template_child]
pub col_lat: TemplateChild<ColumnViewColumn>,
#[template_child]
pub col_lon: TemplateChild<ColumnViewColumn>,
#[template_child]
pub col_elev: TemplateChild<ColumnViewColumn>,
#[template_child]
pub airport_search_name: TemplateChild<Entry>,
#[template_child]
pub airport_search_lat: TemplateChild<Entry>,
#[template_child]
pub airport_search_long: TemplateChild<Entry>,
#[template_child]
pub airport_search: TemplateChild<Button>,
popover: RefCell<Option<PopoverMenu>>,
filter_list_model: RefCell<Option<FilterListModel>>,
}
The #[template(resource brings in the resource
and each #[template_child]
is a reference to each object in the XML efinition of the UI. The names must match up.
jcbritobr (Júlio César de Brito Gardona) March 16, 2025, 12:40pm 3
Hello. Good morning.
I will try this way.
jcbritobr (Júlio César de Brito Gardona) March 16, 2025, 1:00pm 4
What Im seeing here is that I need to do a subclass for my widgets, thats correct? I cant use templates as the gtk4::Button
directly.
system (system) Closed April 30, 2025, 1:00pm 5
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.