Skip to content

Commit 03f6cb8

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 03f6cb8

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
pub use cxx_qt::QObject;
77
use std::pin::Pin;
8+
use cxx_qt::casting::Upcast;
89

910
#[cxx::bridge]
1011
pub mod ffi {
@@ -33,6 +34,10 @@ pub mod ffi {
3334

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

@@ -49,9 +54,11 @@ pub trait QObjectExt {
4954

5055
fn object_name(&self) -> QString;
5156

52-
fn parent(&self) -> *mut Self;
57+
fn parent(&self) -> *mut QObjectExternal;
5358

5459
fn set_parent(self: Pin<&mut Self>, parent: &Self);
60+
61+
fn dump_object_info(&self);
5562
}
5663

5764
/// Used to convert the QObject type from the library type to the C++ type, as a pin
@@ -70,31 +77,35 @@ fn cast(obj: &QObject) -> &QObjectExternal {
7077
}
7178
}
7279

73-
impl QObjectExt for QObject {
80+
impl<T> QObjectExt for T where T: Upcast<QObject> {
7481
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool {
75-
cast_pin(self).block_signals(block)
82+
cast_pin(self.upcast_pin()).block_signals(block)
7683
}
7784

7885
fn signals_blocked(&self) -> bool {
79-
cast(self).signals_blocked()
86+
cast(self.upcast()).signals_blocked()
8087
}
8188

8289
fn set_object_name(self: Pin<&mut Self>, name: &QString) {
83-
cast_pin(self).set_object_name(name)
90+
cast_pin(self.upcast_pin()).set_object_name(name)
8491
}
8592

8693
fn object_name(&self) -> QString {
87-
cast(self).object_name()
94+
cast(self.upcast()).object_name()
8895
}
8996

90-
fn parent(&self) -> *mut Self {
91-
cast(self).parent() as *mut Self
97+
fn parent(&self) -> *mut QObjectExternal {
98+
cast(self.upcast()).parent()
9299
}
93100

94101
fn set_parent(self: Pin<&mut Self>, parent: &Self) {
102+
let s = cast_pin(self.upcast_pin());
95103
unsafe {
96-
cast_pin(self)
97-
.set_parent(cast(parent) as *const QObjectExternal as *mut QObjectExternal);
104+
s.set_parent(cast(parent.upcast()) as *const QObjectExternal as *mut QObjectExternal)
98105
}
99106
}
100-
}
107+
108+
fn dump_object_info(&self) {
109+
cast(self.upcast()).dump_object_info()
110+
}
111+
}

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)