diff --git a/crates/cxx-qt-lib/src/core/qobject.rs b/crates/cxx-qt-lib/src/core/qobject.rs index 0f818865d..80b44f636 100644 --- a/crates/cxx-qt-lib/src/core/qobject.rs +++ b/crates/cxx-qt-lib/src/core/qobject.rs @@ -3,6 +3,7 @@ // // SPDX-License-Identifier: MIT OR Apache-2.0 +use cxx_qt::casting::Upcast; pub use cxx_qt::QObject; use std::pin::Pin; @@ -33,6 +34,9 @@ pub mod ffi { #[rust_name = "set_parent"] pub unsafe fn setParent(self: Pin<&mut Self>, parent: *mut QObjectExternal); + + #[rust_name = "dump_object_info"] + fn dumpObjectInfo(&self); } } @@ -49,9 +53,11 @@ pub trait QObjectExt { fn object_name(&self) -> QString; - fn parent(&self) -> *mut Self; + fn parent(&self) -> *mut QObjectExternal; fn set_parent(self: Pin<&mut Self>, parent: &Self); + + fn dump_object_info(&self); } /// 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 { } } -impl QObjectExt for QObject { +impl QObjectExt for T +where + T: Upcast, +{ fn block_signals(self: Pin<&mut Self>, block: bool) -> bool { - cast_pin(self).block_signals(block) + cast_pin(self.upcast_pin()).block_signals(block) } fn signals_blocked(&self) -> bool { - cast(self).signals_blocked() + cast(self.upcast()).signals_blocked() } fn set_object_name(self: Pin<&mut Self>, name: &QString) { - cast_pin(self).set_object_name(name) + cast_pin(self.upcast_pin()).set_object_name(name) } fn object_name(&self) -> QString { - cast(self).object_name() + cast(self.upcast()).object_name() } - fn parent(&self) -> *mut Self { - cast(self).parent() as *mut Self + fn parent(&self) -> *mut QObjectExternal { + cast(self.upcast()).parent() } fn set_parent(self: Pin<&mut Self>, parent: &Self) { + let s = cast_pin(self.upcast_pin()); unsafe { - cast_pin(self) - .set_parent(cast(parent) as *const QObjectExternal as *mut QObjectExternal); + s.set_parent(cast(parent.upcast()) as *const QObjectExternal as *mut QObjectExternal) } } + + fn dump_object_info(&self) { + cast(self.upcast()).dump_object_info() + } } diff --git a/crates/cxx-qt/src/lib.rs b/crates/cxx-qt/src/lib.rs index 41fbe1668..9ba9e064b 100644 --- a/crates/cxx-qt/src/lib.rs +++ b/crates/cxx-qt/src/lib.rs @@ -9,7 +9,7 @@ //! //! See the [book](https://kdab.github.io/cxx-qt/book/) for more information. -use std::{fs::File, io::Write, path::Path}; +use std::{fs::File, io::Write, path::Path, pin::Pin}; #[doc(hidden)] pub mod casting; @@ -125,6 +125,26 @@ pub use threading::{CxxQtThread, ThreadingQueueError}; #[doc(hidden)] pub use static_assertions; +/// This trait provides wrappers for objects which are QObjects or can be turned into a [QObject]. +/// It is automatically implemented for any types which implement [casting::Upcast] to a [QObject]. +pub trait AsObject { + /// Cast self reference into a [QObject] reference + fn as_qobject(&self) -> &QObject; + + /// Cast pinned mutable reference into a pinned mutable [QObject] reference + fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject>; +} + +impl> AsObject for T { + fn as_qobject(&self) -> &QObject { + self.upcast() + } + + fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject> { + self.upcast_pin() + } +} + /// This trait is automatically implemented for all QObject types generated by CXX-Qt. /// It provides information about the inner Rust struct that is wrapped by the QObject, as well as the methods /// that Cxx-Qt will generate for the QObject. diff --git a/crates/cxx-qt/src/qobject.rs b/crates/cxx-qt/src/qobject.rs index f1f02edf9..588199a83 100644 --- a/crates/cxx-qt/src/qobject.rs +++ b/crates/cxx-qt/src/qobject.rs @@ -14,10 +14,6 @@ mod ffi { /// Most methods available on this type are within the [cxx_qt_lib::core::QObjectExt] trait, /// which needs to be imported in order to access these. type QObject; - - #[cxx_name = "dumpObjectInfo"] - /// Dump information about this QObjects name and signals - fn dump_object_info(&self); } }