Skip to content

Commit b81db2f

Browse files
Add book entry for instantiating in Rust
1 parent 8f56704 commit b81db2f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SPDX-License-Identifier: MIT OR Apache-2.0
2222
- [Nested Objects](./concepts/nested_objects.md)
2323
- [Inheritance & Overriding](./concepts/inheritance.md)
2424
- [Casting](./concepts/casting.md)
25+
- [Instantiating in Rust](./concepts/instantiating_in_rust.md)
2526
- [Reference: the bridge module](./bridge/index.md)
2627
- [`extern "RustQt"`](./bridge/extern_rustqt.md)
2728
- [`extern "C++Qt"`](./bridge/extern_cppqt.md)
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
SPDX-FileContributor: Ben Ford <[email protected]>
4+
5+
SPDX-License-Identifier: MIT OR Apache-2.0
6+
-->
7+
8+
# Instantiating `QObject`s directly in Rust
9+
10+
Your `QObject` types will most likely be instantiated via QML, but it is possible to create them in Rust via a template.
11+
By adding
12+
13+
```rust,ignore
14+
#[namespace = "rust::cxxqtlib1"]
15+
unsafe extern "C++" {
16+
include!("cxx-qt-lib/common.h");
17+
18+
#[cxx_name = "make_unique"]
19+
#[doc(hidden)]
20+
fn myobject_make_unique() -> UniquePtr<MyObject>;
21+
}
22+
```
23+
24+
You can directly create an instance of your object wrapped in a `UniquePtr` within Rust, should you wish.
25+
The included header file contains some wrapper templates for constructing `unique_ptr<T>`, `shared_ptr<T>` and `*T`.
26+
By exposing this to the bridge with the correct namespace, constructing these structs is possible in Rust.
27+
28+
## Possible Methods
29+
30+
| Name | C++ Return Type | Rust Return Type |
31+
|---------------|-----------------|------------------|
32+
| `make_unique` | `unique_ptr<T>` | `UniquePtr<T>` |
33+
| `make_shared` | `shared_ptr<T>` | `SharedPtr<T>` |
34+
| `new_ptr` | `*T` | `*mut T` |

book/src/getting-started/1-qobjects-in-rust.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ In comparison to a normal opaque CXX class, the mapping between the `QObject` su
7070

7171
The `QObject` subclass is its own type in Rust, as well as in C++.
7272
When such a `QObject` is instantiated, it will always also construct an instance of the Rust `struct`.
73+
The `QObject`s Rust struct can also be directly constructed via a `UniquePtr<YourObject>` in Rust, but this is a less common use case,
74+
see [here](../concepts/instantiating_in_rust.md) for more information.
7375
The `QObject` can then refer to the underlying Rust `struct` for property access.
7476
Any behavior of this `QObject` subclass will also be defined in Rust, e.g. using the [`#[qinvokable]`](../bridge/extern_rustqt.html#invokables) attribute.
7577
The Rust implementation also has access to the underlying Rust `struct` to modify any Rust data.

0 commit comments

Comments
 (0)