-
Notifications
You must be signed in to change notification settings - Fork 89
Implement automatic implementations of up and downcasting #1159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ahayzen-kdab
merged 6 commits into
KDAB:main
from
BenFordTytherington:add-up-down-casting
Feb 28, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a7e924
Automatically generate upcasting implementations
BenFordTytherington fb007b4
Add downcasting
BenFordTytherington 5eb9529
Implement basic casting for some lib types
BenFordTytherington 32dfaae
Add casting for QQmlEngine and reexport qobject
BenFordTytherington 845c6f6
Update CHANGELOG
BenFordTytherington db8d215
Update book and docs
BenFordTytherington File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!-- | ||
SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
SPDX-FileContributor: Ben Ford <[email protected]> | ||
|
||
SPDX-License-Identifier: MIT OR Apache-2.0 | ||
--> | ||
|
||
# Casting | ||
|
||
With the [base](../bridge/attributes.md) attribute, it is possible to inherit from another type. | ||
In order to access this parent class, we provide an API to cast up or down. | ||
Currently, this is only supported for objects in `extern "RustQt"` blocks, which have either a `#[qobject]` attribute, | ||
or a `#[base = T]` attribute. see [here](../bridge/attributes.md) for more details on these attributes. | ||
|
||
## Accessing the base class | ||
|
||
To access the methods of a base class in Rust, use the `Upcast` trait like so `use cxx_qt::Upcast;`. | ||
Objects with base classes can then be accessed with the following methods | ||
|
||
| Self Type | Method | | ||
|------------------|----------------| | ||
| `&self` | `upcast()` | | ||
| `&mut self` | `upcast_mut()` | | ||
| `Pin<&mut self>` | `upcast_pin()` | | ||
|
||
This will then return a reference to the base in the same format as the self type, e.g. `upcast()` returns `&Base`, etc... | ||
|
||
## Accessing the child class | ||
|
||
This also works in the opposite direction, allowing access to the child a base class was obtained from. | ||
To do this, use the `Downcast` trait like so `use cxx_qt::Downcast;`. | ||
The child can then be accessed in the same manner, with the following methods | ||
|
||
| Self Type | Method | | ||
|------------------|------------------| | ||
| `&self` | `downcast()` | | ||
| `&mut self` | `downcast_mut()` | | ||
| `Pin<&mut self>` | `downcast_pin()` | | ||
|
||
These will return an `Option<T>`, as it is possible that downcasting will fail, | ||
if the type is not actually of the given subclass, | ||
and these also return in the same format as the self type, e.g. `downcast()` returns `Option<&Sub>`, etc... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.