Skip to content

Commit 2f5dae6

Browse files
Add qobject extension trait
- cxx-qt-lib trait for additional qobject wrappers - adds blockSignals - adds signalsBlocked - adds setObjectName - adds objectName - adds parent - adds setParent
1 parent 64d4ee6 commit 2f5dae6

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

crates/cxx-qt-lib/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fn main() {
150150
"core/qmargins",
151151
"core/qmarginsf",
152152
"core/qmodelindex",
153+
"core/qobject",
153154
"core/qpersistentmodelindex",
154155
"core/qpoint",
155156
"core/qpointf",

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

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub use cxx_qt::{QMetaObjectConnection, QMetaObjectConnectionGuard};
4747
mod qmodelindex;
4848
pub use qmodelindex::QModelIndex;
4949

50+
mod qobject;
51+
pub use qobject::QObjectExt;
52+
5053
mod qpersistentmodelindex;
5154
pub use qpersistentmodelindex::QPersistentModelIndex;
5255

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

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
2+
// SPDX-FileContributor: Ben Ford <[email protected]>
3+
//
4+
// SPDX-License-Identifier: MIT OR Apache-2.0
5+
6+
pub use cxx_qt::QObject;
7+
use std::pin::Pin;
8+
9+
#[cxx_qt::bridge]
10+
mod ffi {
11+
unsafe extern "C++" {
12+
#[rust_name = "QObjectExternal"]
13+
type QObject;
14+
15+
include!("cxx-qt-lib/qstring.h");
16+
type QString = crate::QString;
17+
18+
#[rust_name = "block_signals"]
19+
pub fn blockSignals(self: Pin<&mut QObjectExternal>, block: bool) -> bool;
20+
21+
#[rust_name = "signals_blocked"]
22+
pub fn signalsBlocked(self: &QObjectExternal) -> bool;
23+
24+
#[rust_name = "set_object_name"]
25+
pub fn setObjectName(self: Pin<&mut QObjectExternal>, name: &QString);
26+
27+
#[rust_name = "object_name"]
28+
pub fn objectName(self: &QObjectExternal) -> QString;
29+
30+
pub fn parent(self: &QObjectExternal) -> *mut QObjectExternal;
31+
32+
#[rust_name = "set_parent"]
33+
pub unsafe fn setParent(self: Pin<&mut QObjectExternal>, parent: *mut QObjectExternal);
34+
}
35+
}
36+
37+
use crate::core::qobject::ffi::QString;
38+
use ffi::QObjectExternal;
39+
40+
/// Trait which exposes methods available on a `QObject`.
41+
/// Exposes some basic signals and methods for now, more to be added.
42+
pub trait QObjectExt {
43+
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool;
44+
45+
fn signals_blocked(&self) -> bool;
46+
47+
fn set_object_name(self: Pin<&mut Self>, name: &QString);
48+
49+
fn object_name(&self) -> QString;
50+
51+
fn parent(&self) -> *mut Self;
52+
53+
fn set_parent(self: Pin<&mut Self>, parent: &Self);
54+
}
55+
56+
/// Used to convert the QObject type from the library type to the C++ type, as a pin
57+
fn cast_pin(obj: Pin<&mut QObject>) -> Pin<&mut QObjectExternal> {
58+
unsafe {
59+
let mut_ptr = obj.get_unchecked_mut() as *mut QObject as *mut QObjectExternal;
60+
Pin::new_unchecked(&mut *mut_ptr)
61+
}
62+
}
63+
64+
/// Used to convert the QObject type from the library type to the C++ type
65+
fn cast(obj: &QObject) -> &QObjectExternal {
66+
unsafe {
67+
let ptr = obj as *const QObject as *const QObjectExternal;
68+
&*ptr
69+
}
70+
}
71+
72+
impl QObjectExt for QObject {
73+
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool {
74+
cast_pin(self).block_signals(block)
75+
}
76+
77+
fn signals_blocked(&self) -> bool {
78+
cast(self).signals_blocked()
79+
}
80+
81+
fn set_object_name(self: Pin<&mut Self>, name: &QString) {
82+
cast_pin(self).set_object_name(name)
83+
}
84+
85+
fn object_name(&self) -> QString {
86+
cast(self).object_name()
87+
}
88+
89+
fn parent(&self) -> *mut Self {
90+
cast(self).parent() as *mut Self
91+
}
92+
93+
fn set_parent(self: Pin<&mut Self>, parent: &Self) {
94+
unsafe {
95+
cast_pin(self)
96+
.set_parent(cast(parent) as *const QObjectExternal as *mut QObjectExternal);
97+
}
98+
}
99+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<'de> serde::Deserialize<'de> for QString {
434434

435435
struct StringVisitor;
436436

437-
impl<'de> Visitor<'de> for StringVisitor {
437+
impl Visitor<'_> for StringVisitor {
438438
type Value = QString;
439439

440440
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {

crates/cxx-qt/src/qobject.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
#[cxx::bridge]
99
mod ffi {
1010
unsafe extern "C++" {
11-
// TODO! Implement QObject wrapper properly
1211
include!(<QtCore/QObject>);
13-
/// QObject type
12+
/// QObject type.
13+
///
14+
/// Most methods available on this type are within the [cxx_qt_lib::core::QObjectExt] trait,
15+
/// which needs to be imported in order to access these.
1416
type QObject;
1517

1618
#[cxx_name = "dumpObjectInfo"]

0 commit comments

Comments
 (0)