qsignal

Attribute Macro qsignal 

Source
#[qsignal]
Expand description

Annotates a function as a signal that can be handled in QML.

Signals can be called from Rust and the signal handler can be defined in QML. This is the recommended way to invoke QML code from Rust.

§Requirements

  • The signal must be defined within a mod or impl block, annotated with qobject or qobject_impl, respectively.
  • The first argument of the annotated function must be &self or &mut self.
  • All other parameter types and the return type must be one of the supported types.
  • The function must not have a body (end with semicolon or have an empty curly braces).
#[qobject_impl]
impl Backend {
    #[qsignal]
    fn value_changed(&self, new_value: i32);
    #[qsignal]
    fn event_triggered(&self){}
}

To receive a notification on the QML side, the object definition has to declare a signal handler named on<Signal>, where <Signal> is the name of the signal, with the first letter capitalized.

Backend {
    onValue_changed: console.log("Value changed");
}

Alternatively you can instantiate a Connection object with the respective signal handler.

Connection {
    target: backend
    function onValue_changed() {
        console.log("Value changed");
    }
}

For more details see https://doc.qt.io/qt-6/qtqml-syntax-signals.html

§Parameters

qml_name

The signal name as seen in QML. Defaults to the Rust function name.