pub struct QThreadHandle<T> { /* private fields */ }Expand description
A thread-safe handle that schedules Rust closures to run on the Qt thread.
A QThreadHandle<T> is bound to a #[qobject] instance of type T and
hands the closure access to it: shared via invoke,
mutable via invoke_mut. Obtain one via
QmlObject::get_thread_handle.
The calls are type-checked at compile time and need no #[qslot] annotation.
A QThreadHandle<T> has to be created on the Qt thread and can only be bound
to #[qobject] instances that live on that thread.
A QThreadHandle<()>, created via QThreadHandle::default, is not
bound to any #[qobject] instance and just runs closures on the Qt thread.
It can be created in any thread.
Closures are posted to the Qt event loop through the application instance; On delivery the target’s liveness is checked on the Qt thread, so a target deleted at any point is safe: the closure is then silently discarded. Without an application, closures are discarded as well, and so are the closures of a handle whose target was created on another thread.
While a closure runs, it holds a borrow of the Rc<RefCell<_>> shared
with the QML engine. If the object is already mutably borrowed at that
point (for example by a slot running a nested event loop), the application
is aborted.
§Example
let backend = Backend::default_with_attached_qobject();
let handle = backend.borrow().get_thread_handle();
std::thread::spawn(move || {
handle.invoke_mut(|b| {
b.value = 42;
b.data_ready();
});
}).join().unwrap();Implementations§
Source§impl QThreadHandle<()>
impl QThreadHandle<()>
Sourcepub fn invoke(&self, callback: impl FnOnce() + Send + 'static)
pub fn invoke(&self, callback: impl FnOnce() + Send + 'static)
Schedules callback in the Qt event loop.
If there is no application, the callback is silently discarded, along with all captured objects.
§Example
let handle = QThreadHandle::default();
std::thread::spawn(move || {
handle.invoke(|| println!("on the Qt thread"));
QThreadHandle::default().invoke(|| println!("also on the Qt thread"));
}).join().unwrap();Source§impl<T> QThreadHandle<T>where
T: QObjectHolder,
impl<T> QThreadHandle<T>where
T: QObjectHolder,
Sourcepub fn new(target: &T) -> QThreadHandle<T>
pub fn new(target: &T) -> QThreadHandle<T>
Creates a QThreadHandle for target.
Prefer
QmlObject::get_thread_handle
over calling this directly.
Sourcepub fn invoke(&self, callback: impl FnOnce(&T) + Send + 'static)
pub fn invoke(&self, callback: impl FnOnce(&T) + Send + 'static)
Schedules callback in the Qt event loop with shared access to the
bound target.
If the target has been dropped by the time the event loop delivers the call the callback is silently discarded, along with all captured objects.
Sourcepub fn invoke_mut(&self, callback: impl FnOnce(&mut T) + Send + 'static)
pub fn invoke_mut(&self, callback: impl FnOnce(&mut T) + Send + 'static)
Schedules callback in the Qt event loop with mutable access to the
target.
If the target has been dropped by the time the event loop delivers the call the callback is silently discarded, along with all captured objects.
Trait Implementations§
Source§impl Default for QThreadHandle<()>
impl Default for QThreadHandle<()>
Source§fn default() -> QThreadHandle<()>
fn default() -> QThreadHandle<()>
Creates a handle without a target. May be called on any thread.