Gtk-rs - Get the width of a widget in Subclass

Hi, I have a question. I’m creating a GUI application in Rust that uses GTK4 and I’m creating a custom widget, but I’d like to have the size of my widget displayed on the screen in my widget’s subclass. How can I do this?

#[derive(CompositeTemplate, Default)]
#[template(resource = "/glushkovizer.ui")]
pub struct GlushkovizerApp {
    #[template_child]
    pub entry: TemplateChild<Entry>,
    #[template_child]
    pub image: TemplateChild<Image>,
}

#[glib::object_subclass]
impl ObjectSubclass for GlushkovizerApp {
    const NAME: &'static str = "GlushkovizerApp";
    type Type = super::GlushkovizerApp;
    type ParentType = gtk::ApplicationWindow;

    fn class_init(klass: &mut Self::Class) {
        klass.bind_template();
        klass.bind_template_callbacks();
    }

    fn instance_init(obj: &InitializingObject<Self>) {
        obj.init_template();
    }
}

impl GlushkovizerApp {
    fn toto(&self) {
      let width = ;// screen size of my widget (with idk self.function_unbelievable())
    }
}

With something like this. I don’t know if it’s possible

Hi,

In C we use Gtk.Widget.get_width , this method should be inherited from ApplicationWindow as it’s a Widget subclass.

I’ve no idea how to use that in Rust, however…

For people who are also blocked I found by going through the method :

fn obj(&self) -> crate::BorrowedObject<Self::Type>
Returns the corresponding object instance.

Shorter alias for instance().

And with the method

println!("{} {}", self.obj().width(), self.obj().height());
1 Like