Skip to main content

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.
  • The first argument of the annotated function must be &mut self.
  • All other parameter types and the return type must implement QMetaCallArg.
  • The function must not have a body (end with a semicolon or empty curly braces {}).
#[qobject]
impl Backend {
    #[qsignal]
    fn value_changed(&mut self, new_value: i32);
    #[qsignal]
    fn event_triggered(&mut 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. Note that the rest of the function name is not affected and the signal handler for e.g. value_changed will be onValue_changed.

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.