pub trait QmlObject: QObjectHolder {
// Provided methods
fn default_with_attached_qobject() -> Rc<RefCell<Self>>
where Self: Default { ... }
fn attach_qobject(instance: &Rc<RefCell<Self>>) { ... }
fn detach_qobject(&self) { ... }
fn get_qml_method_invoker(&self) -> QmlMethodInvoker { ... }
}Expand description
Basic functionality for #[qobject] types.
This trait connects structs to QML and manages their lifetime under
QML usage. This trait is available on every #[qobject] type.
Do not implement this trait manually.
§Object lifetime and ownership
A #[qobject] value is a plain Rust value that you can interact normally
with. In order to allow the QML engine to interact with it, it needs to be
wrapped in an Rc<RefCell<_>>. QtBridge clones this shared reference,
keeps it alive while in use by QML and borrows references to call into
Rust code.
QtBridge requires all #[qobject] types to have a proxy QObject on
the QML side. It is attached lazily on its first exposure, or eagerly
with QmlObject::default_with_attached_qobject and
QmlObject::attach_qobject.
The QObject proxy has to follow the Qt lifetime concept: Parents and
Components delete their children in their destructor.
An object whose QObject was deleted remains a fully usable Rust value.
The interactions with Qt (emitting signals, updating model views) become
a no-op. On its next exposure to QML a fresh QObject is attached.
Connections, bindings and QML references to the old QObject are not
restored.
An object no longer referenced from Rust or reachable from QML is freed
by collect_garbage, which runs automatically after each QML
garbage collection and during allocation pressure.
Provided Methods§
Sourcefn default_with_attached_qobject() -> Rc<RefCell<Self>>where
Self: Default,
fn default_with_attached_qobject() -> Rc<RefCell<Self>>where
Self: Default,
Creates a default-initialized instance and attaches its [QObject]
eagerly.
The returned Rc<RefCell<Self>> is an ordinary handle, shared with
QML. Droping the handle does not drop the instance if it is in use by
QML or until the garbage collection delete the QML instance.
Sourcefn attach_qobject(instance: &Rc<RefCell<Self>>)
fn attach_qobject(instance: &Rc<RefCell<Self>>)
Attaches a dedicated [QObject] to an existing instance,
enabling its use in QML.
Sourcefn detach_qobject(&self)
fn detach_qobject(&self)
Detaches and deletes the dedicated [QObject] of this instance.
The instance continues as a plain Rust value and heals with a
fresh [QObject] on its next exposure to QML.
Sourcefn get_qml_method_invoker(&self) -> QmlMethodInvoker
fn get_qml_method_invoker(&self) -> QmlMethodInvoker
Returns a QmlMethodInvoker that can invoke methods on the underlying
QObject from any thread.
§Example
let backend = Backend::default_with_attached_qobject();
let invoker = backend.borrow().get_qml_method_invoker();
invoker.invoke_method("dataReady");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.