Skip to content

Commit c7ad9d6

Browse files
Add book entry for instantiating in Rust
1 parent 626371a commit c7ad9d6

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-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)
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
These helper methods live in cxx-qt-lib, and thus need to be included, and this is also why the namespace is necessary.
28+
These are helper functions defined by CXX-Qt, and are ***Not the same as*** `std::make_unique`, etc...
29+
30+
## Passing Parameters
31+
32+
You can also supply the constructor with parameters via these helper methods,
33+
but you should ensure that any constructors with different arguments have different names in Rust, via renaming.
34+
35+
```rust,ignore
36+
#[namespace = "rust::cxxqtlib1"]
37+
unsafe extern "C++" {
38+
include!("cxx-qt-lib/common.h");
39+
40+
#[rust_name = "new_my_object_with_parent"]
41+
fn make_unique(parent: *mut QObject) -> UniquePtr<MyObject>;
42+
43+
// Overloading without the parent parameter
44+
#[rust_name = "new_my_object"]
45+
fn make_unique() -> UniquePtr<MyObject>;
46+
}
47+
```
48+
49+
50+
51+
## Possible Methods
52+
53+
| Name | C++ Return Type | Rust Return Type |
54+
|---------------|-----------------|------------------|
55+
| `make_unique` | `unique_ptr<T>` | `UniquePtr<T>` |
56+
| `make_shared` | `shared_ptr<T>` | `SharedPtr<T>` |
57+
| `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)