QmlRegister

Trait QmlRegister 

Source
pub trait QmlRegister:
    QMetaTypeGet
    + QMetaInfo
    + QObjectHolder
    + Default {
    const URI: &'static str;
    const ELEMENT_NAME: &'static str;
    const MINOR_VERSION: u8;
    const MAJOR_VERSION: u8;
    const IS_SINGLETON: bool;

    // Provided method
    fn register() { ... }
}
Expand description

QmlRegister enables QML to instantiate types of this trait.

The trait is usually implemented by qobject and qobject_impl. If you want to implement this trait manually, you have to add the NoQmlElement option.

QmlRegister defines the ELEMENT_NAME with which the struct can be instantiated in QML and the module name, URI, which has to be used as import in QML to use this struct.

QmlRegister knows two ways of registering a type. The ordinary way is to register as an element that can be instantiated in QML:

#[qobject_impl(NoQmlElement)]
impl Backend {
    #[qslot]
    fn say_hello(&self) {
        println!("Hello World!")
    }
}
impl qtbridge::qtbridge_runtime::QmlRegister for Backend {
    const URI: &str = "rust_backend";
    const ELEMENT_NAME: &str = "Backend";
    const MINOR_VERSION: u8 = 0u8;
    const MAJOR_VERSION: u8 = 1u8;
    const IS_SINGLETON: bool = false;
}
import rust_backend
Backend {
    id: backend
}
Button {
    anchors.centerIn: parent
    text: "Hello World!"
    onClicked: backend.sayHello()
}

Alternatively, by setting IS_SINGLETON to true, the type is registered as a singleton. That means that only one instance can be created. It can be accessed with the ELEMENT_NAME:

#[qobject_impl(NoQmlElement)]
impl Backend {
    #[qslot]
    fn say_hello(&self) {
        println!("Hello World!")
    }
}
impl qtbridge::qtbridge_runtime::QmlRegister for Backend {
    const URI: &str = "rust_backend";
    const ELEMENT_NAME: &str = "Backend";
    const MINOR_VERSION: u8 = 0u8;
    const MAJOR_VERSION: u8 = 1u8;
    const IS_SINGLETON: bool = true;
}
import rust_backend
Button {
    anchors.centerIn: parent
    text: "Hello World!"
    onClicked: Backend.sayHello()
}

Further, MAJOR_VERSION and MINOR_VERSION define the version of the QML module. These fields are mandatory but QML can load a module without specifying the version

Required Associated Constants§

Provided Methods§

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§