Skip to content

Commit 33bfa62

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 33bfa62

File tree

6 files changed

+119
-4
lines changed

6 files changed

+119
-4
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",
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
// clang-format on
4+
// SPDX-FileContributor: Ben Ford <[email protected]>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
#pragma once
8+
9+
#include <QtCore/QObject>

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

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

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
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"]
17-
/// Dump Object info method, added so that upcast methods can be tested.
19+
/// Dump information about this QObjects name and signals
1820
fn dump_object_info(&self);
1921
}
2022
}

0 commit comments

Comments
 (0)