Clone TemplateChild<Button> into glib::spawn_future_local

I want to set a button to inactive in a future exactly like in this tutorial, I however want to use a button which I defined in a ui file. However when I try to implement it like in the tutorial:

let button = self.imp().generate_icon;
glib::spawn_future_local(clone!(@weak button => async move {
    while let Ok(enable_button) = rx.recv().await {
        button.set_sensitive(enable_button);
    }
}));

I get the following error:

error[E0277]: the trait bound `gtk4::TemplateChild<gtk4::Button>: Downgrade` is not satisfied
   --> src/window.rs:164:34
    |
164 |           glib::spawn_future_local(clone!(@weak button => async move {
    |  __________________________________^
165 | |             while let Ok(enable_button) = rx.recv().await {
166 | |                 button.set_sensitive(enable_button);
167 | |             }
168 | |         }));
    | |__________^ the trait `Downgrade` is not implemented for `gtk4::TemplateChild<gtk4::Button>`
    |
    = help: the following other types implement trait `Downgrade`:
              application::imp::GtkTestApplication
              application::GtkTestApplication
              window::imp::GtkTestWindow
              window::GtkTestWindow
              Pixbuf
              PixbufAnimation
              PixbufLoader
              PixbufNonAnim
            and 541 others
    = note: this error originates in the macro `clone` (in Nightly builds, run with -Z macro-backtrace for more info)

How do I implement this correctly, because I can’t manage to figure it out.

Many thanks :slight_smile:

Please create an issue for adding a Downgrade implementation to gtk4::TemplateChild.

For now you’d have to do something like

let button = self.imp().generate_icon.get(); // <-------- note the addition of `get()`
glib::spawn_future_local(clone!(@weak button => async move {
    while let Ok(enable_button) = rx.recv().await {
        button.set_sensitive(enable_button);
    }
}));

Implemented it here gtk: Implement Downgrade for TemplateChild<T> by bilelmoussaoui · Pull Request #1750 · gtk-rs/gtk4-rs · GitHub

1 Like

thank you for the response, that seems to have worked!

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