Skip to main content

QmlElement

Trait QmlElement 

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

    // Provided methods
    fn get_qmetatype() -> QMetaType { ... }
    fn get_list_qmetatype() -> QMetaType { ... }
    fn list_to_qvariant<Owner, Notify>(
        owner: &Owner,
        store: &Vec<Rc<RefCell<Self>>>,
        _notify: Notify,
    ) -> QVariant
       where Owner: QObjectHolder,
             Notify: Fn(&mut Owner) + 'static { ... }
    fn register() { ... }
}
Expand description

QmlElement enables QML to instantiate types of this trait.

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

QmlElement 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.

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

#[qobject(NoQmlElement)]
impl Backend {
    #[qslot]
    fn say_hello(&self) {
        println!("Hello World!")
    }
}
impl qtbridge::qtbridge_runtime::QmlElement 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(NoQmlElement)]
impl Backend {
    #[qslot]
    fn say_hello(&self) {
        println!("Hello World!")
    }
}
impl qtbridge::qtbridge_runtime::QmlElement 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§

Source

fn get_qmetatype() -> QMetaType

Returns the QMetaType identifying this type, unique per concrete type.

The #[qobject] macro overrides this with a per-type OnceLock body; the default serves generic types and hand-written impls.

Source

fn get_list_qmetatype() -> QMetaType

Source

fn list_to_qvariant<Owner, Notify>( owner: &Owner, store: &Vec<Rc<RefCell<Self>>>, _notify: Notify, ) -> QVariant
where Owner: QObjectHolder, Notify: Fn(&mut Owner) + 'static,

Source

fn register()

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§