Skip to content

Commit b010ed1

Browse files
Add blanket implementation of QObjectExt
- All objects which upcast to QObject now have QObjectExt implemented - Via transitive casting and using the QObjectExt trait, any descendent of QObject can access its methods
1 parent 9fec352 commit b010ed1

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

crates/cxx-qt-lib/src/core/qobject.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6+
use cxx_qt::casting::Upcast;
67
pub use cxx_qt::QObject;
78
use std::pin::Pin;
89

@@ -33,6 +34,9 @@ pub mod ffi {
3334

3435
#[rust_name = "set_parent"]
3536
pub unsafe fn setParent(self: Pin<&mut Self>, parent: *mut QObjectExternal);
37+
38+
#[rust_name = "dump_object_info"]
39+
fn dumpObjectInfo(&self);
3640
}
3741
}
3842

@@ -49,9 +53,11 @@ pub trait QObjectExt {
4953

5054
fn object_name(&self) -> QString;
5155

52-
fn parent(&self) -> *mut Self;
56+
fn parent(&self) -> *mut QObjectExternal;
5357

5458
fn set_parent(self: Pin<&mut Self>, parent: &Self);
59+
60+
fn dump_object_info(&self);
5561
}
5662

5763
/// Used to convert the QObject type from the library type to the C++ type, as a pin
@@ -70,31 +76,38 @@ fn cast(obj: &QObject) -> &QObjectExternal {
7076
}
7177
}
7278

73-
impl QObjectExt for QObject {
79+
impl<T> QObjectExt for T
80+
where
81+
T: Upcast<QObject>,
82+
{
7483
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool {
75-
cast_pin(self).block_signals(block)
84+
cast_pin(self.upcast_pin()).block_signals(block)
7685
}
7786

7887
fn signals_blocked(&self) -> bool {
79-
cast(self).signals_blocked()
88+
cast(self.upcast()).signals_blocked()
8089
}
8190

8291
fn set_object_name(self: Pin<&mut Self>, name: &QString) {
83-
cast_pin(self).set_object_name(name)
92+
cast_pin(self.upcast_pin()).set_object_name(name)
8493
}
8594

8695
fn object_name(&self) -> QString {
87-
cast(self).object_name()
96+
cast(self.upcast()).object_name()
8897
}
8998

90-
fn parent(&self) -> *mut Self {
91-
cast(self).parent() as *mut Self
99+
fn parent(&self) -> *mut QObjectExternal {
100+
cast(self.upcast()).parent()
92101
}
93102

94103
fn set_parent(self: Pin<&mut Self>, parent: &Self) {
104+
let s = cast_pin(self.upcast_pin());
95105
unsafe {
96-
cast_pin(self)
97-
.set_parent(cast(parent) as *const QObjectExternal as *mut QObjectExternal);
106+
s.set_parent(cast(parent.upcast()) as *const QObjectExternal as *mut QObjectExternal)
98107
}
99108
}
109+
110+
fn dump_object_info(&self) {
111+
cast(self.upcast()).dump_object_info()
112+
}
100113
}

crates/cxx-qt/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! See the [book](https://kdab.github.io/cxx-qt/book/) for more information.
1111
12-
use std::{fs::File, io::Write, path::Path};
12+
use std::{fs::File, io::Write, path::Path, pin::Pin};
1313

1414
#[doc(hidden)]
1515
pub mod casting;
@@ -125,6 +125,26 @@ pub use threading::{CxxQtThread, ThreadingQueueError};
125125
#[doc(hidden)]
126126
pub use static_assertions;
127127

128+
/// This trait provides wrappers for objects which are QObjects or can be turned into a [QObject].
129+
/// It is automatically implemented for any types which implement [casting::Upcast] to a [QObject].
130+
pub trait AsObject {
131+
/// Cast self reference into a [QObject] reference
132+
fn as_qobject(&self) -> &QObject;
133+
134+
/// Cast pinned mutable reference into a pinned mutable [QObject] reference
135+
fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject>;
136+
}
137+
138+
impl<T: casting::Upcast<QObject>> AsObject for T {
139+
fn as_qobject(&self) -> &QObject {
140+
self.upcast()
141+
}
142+
143+
fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject> {
144+
self.upcast_pin()
145+
}
146+
}
147+
128148
/// This trait is automatically implemented for all QObject types generated by CXX-Qt.
129149
/// It provides information about the inner Rust struct that is wrapped by the QObject, as well as the methods
130150
/// that Cxx-Qt will generate for the QObject.

crates/cxx-qt/src/qobject.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ mod ffi {
1414
/// Most methods available on this type are within the [cxx_qt_lib::core::QObjectExt] trait,
1515
/// which needs to be imported in order to access these.
1616
type QObject;
17-
18-
#[cxx_name = "dumpObjectInfo"]
19-
/// Dump information about this QObjects name and signals
20-
fn dump_object_info(&self);
2117
}
2218
}
2319

0 commit comments

Comments
 (0)