slint-ui/slint · error

Cannot set callback

Error message

Cannot set callback {name}: {e}

What it means

Panic in live-preview's reload_properties_and_callbacks when set_global_callback fails after a reload. A callback previously registered on a global singleton could not be re-attached to the new component instance.

Solutions

  1. Keep the global callback declaration (name and signature) unchanged across the reload.
  2. Inspect {e} to find the exact missing global/callback and fix the .slint source or re-register with the new name.
  3. Restart the live preview session to drop stale callback registrations.
  4. Re-set callbacks after reload rather than relying on automatic re-registration when signatures change.

Example fix

// before (global callback removed during reload)
export global Actions { callback save(); }
// after: keep it declared or update registrations
export global Actions { callback save(); } // and re-register set_global_callback("Actions", "save", ...)
Defensive patterns

Strategy: fallback

Validate before calling

// Confirm the global and callback exist before re-registering
// instance.definition().globals() / global callback lists

Try / catch

if let Err(e) = instance.set_global_callback(global, prop, handler) {
    eprintln!("skipping stale callback {name}: {e}");
}

Prevention

When it happens

Trigger: Live-preview reload where a global callback name (containing a '.') no longer exists on the reloaded component or its signature changed.

Common situations: Renaming or deleting a global callback during live editing; changing a callback's parameter types; moving the callback out of the global in the .slint source.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@3a7e700487 (2026-09-16). Data as JSON: /api/errors/76388938368b6f40. Report an issue: GitHub.

Appendix: source

Thrown at internal/live-preview/live_component.rs:152

    pub fn reload_properties_and_callbacks(&self) {
        // Set the properties
        for (name, value) in self.properties.borrow_mut().iter() {
            if let Some((global, prop)) = name.split_once('.') {
                self.instance()
                    .set_global_property(global, prop, value.clone())
                    .unwrap_or_else(|e| panic!("Cannot set property {name}: {e}"));
            } else {
                self.instance()
                    .set_property(name, value.clone())
                    .unwrap_or_else(|e| panic!("Cannot set property {name}: {e}"));
            }
        }
        for (name, callback) in self.callbacks.borrow_mut().iter() {
            let callback = callback.clone();
            if let Some((global, prop)) = name.split_once('.') {
                self.instance()
                    .set_global_callback(global, prop, move |args| callback(args))
                    .unwrap_or_else(|e| panic!("Cannot set callback {name}: {e}"));
            } else {
                self.instance()
                    .set_callback(name, move |args| callback(args))
                    .unwrap_or_else(|e| panic!("Cannot set callback {name}: {e}"));
            }
        }

        eprintln!(
            "Reloaded component {} from {}",
            self.component_name.as_deref().unwrap_or("<default>"),
            self.file_name.display()
        );
    }

    /// Set a hook that runs after each successful reload, receiving the new instance.
    pub fn set_post_reload_hook(&mut self, hook: impl Fn(&ComponentInstance) + 'static) {
        self.post_reload_hook = Some(Box::new(hook));
    }

View on GitHub (pinned to 3a7e700487)