|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> |
| 3 | +SPDX-FileContributor: Andrew Hayzen <[email protected]> |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT OR Apache-2.0 |
| 6 | +--> |
| 7 | + |
| 8 | +# `qobject::T` - The generated QObject |
| 9 | + |
| 10 | +One of the key features of CXX-Qt is the ability to create your own QObjects from Rust. |
| 11 | +This is what the [`#[cxx_qt::qobject]` macro](./qobject_struct.md) is for. |
| 12 | +This page serves to document the details of what is generated and how to interact with the generated QObject from Rust. |
| 13 | + |
| 14 | +The `#[cxx_qt::qobject]` macro generates a QObject for a given Rust struct. |
| 15 | +Whilst this QObject is a C++ type, CXX-Qt will automatically wrap it as a [CXX Opaque Type](https://cxx.rs/extern-c++.html#opaque-c-types). |
| 16 | +These generated QObjects are accessible to Rust in a generated module with the name `qobject`. Each struct `T`'s generated QObject is accessible as `qobject::T`. |
| 17 | + |
| 18 | +## Anatomy |
| 19 | + |
| 20 | +Any QObject generated by CXX-Qt is just a C++ QObject subclass that owns an instance of the Rust struct. |
| 21 | +The instance of the Rust struct is constructed using its [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) implementation in the C++ constructor. |
| 22 | +Therefore implementing `Default` on the Rust struct is currently mandatory. |
| 23 | + |
| 24 | +The C++ object will defer any property state to the Rust struct, and is therefore only a thin wrapper. |
| 25 | +The data for `Q_PROPERTY`s is stored in the Rust struct. The property getters and setters modify the Rust struct internally. |
| 26 | + |
| 27 | +Additionally, CXX-Qt generates methods on this struct that help you interact with Qt functionality. |
| 28 | +This includes access to the [`CxxQtThread` struct](./cxxqtthread.md), as well as a function to emit signals. |
| 29 | + |
| 30 | +## Referencing another QObject |
| 31 | + |
| 32 | +The `qobject::T` type is defined both within the CXX-Qt bridge, as well as the surrounding module. |
| 33 | +The simplest way to reference it is from the surrounding module. |
| 34 | + |
| 35 | +Example: |
| 36 | +``` rust,ignore,noplayground |
| 37 | +// In file qt_types.rs |
| 38 | +#[cxx_qt::bridge] |
| 39 | +mod ffi { |
| 40 | + #[cxx_qt::qobject] |
| 41 | + #[derive(Default)] |
| 42 | + pub struct MyObject {} |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +``` rust,ignore,noplayground |
| 47 | +// In another module |
| 48 | +fn my_function(parameter: &crate::qt_types::qobject::MyObject) { /* ... */ } |
| 49 | +``` |
| 50 | + |
| 51 | +Unfortunately, nested QObjects aren't yet supported by CXX-Qt. |
| 52 | +Progress can be tracked in [#299](https://github.com/KDAB/cxx-qt/issues/299). |
| 53 | + |
| 54 | +## `impl qobject::T` |
| 55 | +As mentioned before, the C++ QObject is exposed to Rust as an opaque CXX type. |
| 56 | +This allows you to implement methods for it in Rust using normal `impl` blocks. |
| 57 | +Because `qobject::T` is an opaque C++ type, the same rules as with normal CXX opaque types apply. |
| 58 | +Most importantly this means that the type may never be accessed by value or by mutable self reference directly. |
| 59 | +Rather, all methods must use either `&self` or `self: Pin<&mut Self>` as the self types. This prevents Rust from moving the data in memory, which would invalidate C++ pointers to it. |
| 60 | + |
| 61 | +For more information about pinning, refer to the [pin documentation](https://doc.rust-lang.org/std/pin/). |
| 62 | + |
| 63 | +In addition to methods that extend the Rust interface of a `qobject::T`, you may also mark methods within an `impl qobject::T` block with the `#[qinvokable]` attribute. |
| 64 | +These methods will be exposed to the C++ QObject and can be called by C++ or QML. |
| 65 | +See the [QObject page](./qobject_struct.md#invokables) for more details. |
| 66 | + |
| 67 | +## Available Functions |
| 68 | + |
| 69 | +Every `qobject::T` struct provides the following methods: |
| 70 | + |
| 71 | +### Access to the Qt thread |
| 72 | +``` rust,ignore,noplayground |
| 73 | +fn qt_thread(&self) -> UniquePtr<CxxQtThread> |
| 74 | +``` |
| 75 | +This function provides you with a handle to the Qt thread that the QObject resides in. |
| 76 | +This is helpful as the QObject itself does not implement [Send](https://doc.rust-lang.org/std/marker/trait.Send.html) nor [Sync](https://doc.rust-lang.org/std/marker/trait.Sync.html). |
| 77 | +The CxxQtThread however is Send and can therefore be moved into a different thread. |
| 78 | +By using the CxxQtThread's `queue` method, you may then queue a Rust closure onto the Qt thread again. |
| 79 | +The closure also takes a pinned mutable reference to the QObject, so that it can modify it. |
| 80 | + |
| 81 | +See the [CxxQtThread page](./cxxqtthread.md) for more details. |
| 82 | + |
| 83 | +### Signal emission |
| 84 | +``` rust,ignore,noplayground |
| 85 | +fn emit(self: Pin<&mut Self>, signal: /*Your Signals enum goes here*/) |
| 86 | +``` |
| 87 | +If there is a [Signals enum](./signals_enum.md) defined, CXX-Qt will generate the appropriate `emit` function to allow you to emit signals. |
| 88 | + |
| 89 | +See the [Signals enum page](./signals_enum.md) for more details. |
| 90 | + |
| 91 | +### Access to internal Rust struct |
| 92 | +For every field in the Rust struct, CXX-Qt will generate appropriate getters and setters. |
| 93 | +See the [QObject page](./qobject_struct.md#properties) for details. |
| 94 | + |
| 95 | +There is also an advanced way to access the data in the internal Rust struct: |
| 96 | +``` rust,ignore,noplayground |
| 97 | +fn rust(&self) -> &T |
| 98 | +unsafe fn rust_mut(self: Pin<&mut Self>) -> &mut T |
| 99 | +``` |
| 100 | +Where `T` is the struct with the `#[cxx_qt::qobject]` macro. |
| 101 | + |
| 102 | +This allows you to directly manipulate the internal Rust struct without having to use the generated accessor methods. |
| 103 | + |
| 104 | +You may notice that the mutable version of this is `unsafe` to call. |
| 105 | +This is by design. |
| 106 | +Modifying a field that corresponds to a `#[qproperty]` without calling the appropriate changed signal may cause a logic error in C++/QML code. |
| 107 | +Therefore all direct access to a struct that is wrapped in a QObject is unsafe! |
| 108 | + |
| 109 | +You may modify the struct and then manually call the required changed signals. |
| 110 | + |
| 111 | +For safe access, prefer using the generated accessor methods for both [properties](./qobject_struct.md#properties), as well as [normal fields](./qobject_struct.md#private-methods-and-fields). |
0 commit comments