Skip to main content

QThreadHandle

Struct QThreadHandle 

Source
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<()>

Source

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: QObjectHolder> QThreadHandle<T>

Source

pub fn new(target: &T) -> Self

Creates a QThreadHandle for target.

Prefer QmlObject::get_thread_handle over calling this directly.

Source

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.

Source

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<()>

Source§

fn default() -> Self

Creates a handle without a target. May be called on any thread.

Source§

impl<T> Send for QThreadHandle<T>

Source§

impl<T> Sync for QThreadHandle<T>

Auto Trait Implementations§

§

impl<T> Freeze for QThreadHandle<T>

§

impl<T> RefUnwindSafe for QThreadHandle<T>

§

impl<T> Unpin for QThreadHandle<T>

§

impl<T> UnsafeUnpin for QThreadHandle<T>

§

impl<T> UnwindSafe for QThreadHandle<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.