Skip to content

Add AsQobject trait #1262

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions crates/cxx-qt-lib/src/core/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
Expand All @@ -70,31 +76,38 @@ fn cast(obj: &QObject) -> &QObjectExternal {
}
}

impl QObjectExt for QObject {
impl<T> QObjectExt for T
where
T: Upcast<QObject>,
{
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()
}
}
22 changes: 21 additions & 1 deletion crates/cxx-qt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T: casting::Upcast<QObject>> 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.
Expand Down
4 changes: 0 additions & 4 deletions crates/cxx-qt/src/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading