Connect signals using Rust object builders?

Is it possible to connect signals using object builders in gtk-rs?

It would be nice to be able to do this:

impl MyApplication {
    pub fn new() -> Self {
        glib::Object::builder()
            .property("application-id", "test.test.t1")
            .connect("activate", Self::on_activate)
            .build()
    }

Right now, it complains about unsatisfied constraints. More complete example below:

use gtk::prelude::*;
use gtk::*;

glib::wrapper! {
    pub struct MyApplication(ObjectSubclass<imp::MyApplication>)
        @extends gio::Application, gtk::Application,
        @implements gio::ActionGroup, gio::ActionMap;
}

impl MyApplication {
    pub fn new() -> Self {
        let me = glib::Object::builder::<Self>()
            .property("application-id", "test.test.t1")
            .connect("activate", Self::on_activate) // <-- ?
            .build();
        // me.connect_activate(Self::on_activate);
        me
    }
    fn on_activate(&self) {    }
}

impl Default for MyApplication {
    fn default() -> Self { Self::new() }
}

mod imp {
    use gtk::subclass::prelude::*;
    use gtk::*;

    #[derive(Debug, Default)]
    pub struct MyApplication {}

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

    impl ObjectImpl for MyApplication {}
    impl ApplicationImpl for MyApplication {}
    impl GtkApplicationImpl for MyApplication {}
}

That’s not possible right now but sounds like an interesting idea. Can you create an issue in github about that?

If you’re already subclassing GApplication, don’t connect to signals: override the virtual function, instead.

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