Skip to content

Commit 85550c4

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 626371a commit 85550c4

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

crates/cxx-qt-lib/build.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
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+
pub trait QObjectExt {
41+
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool;
42+
43+
fn signals_blocked(&self) -> bool;
44+
45+
fn set_object_name(self: Pin<&mut Self>, name: &QString);
46+
47+
fn object_name(&self) -> QString;
48+
49+
fn parent(&self) -> *mut Self;
50+
51+
fn set_parent(self: Pin<&mut Self>, parent: &Self);
52+
}
53+
54+
fn cast_pin(obj: Pin<&mut QObject>) -> Pin<&mut QObjectExternal> {
55+
unsafe {
56+
let mut_ptr = obj.get_unchecked_mut() as *mut QObject as *mut QObjectExternal;
57+
Pin::new_unchecked(&mut *mut_ptr)
58+
}
59+
}
60+
61+
fn cast(obj: &QObject) -> &QObjectExternal {
62+
unsafe {
63+
let ptr = obj as *const QObject as *const QObjectExternal;
64+
&*ptr
65+
}
66+
}
67+
68+
impl QObjectExt for QObject {
69+
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool {
70+
cast_pin(self).block_signals(block)
71+
}
72+
73+
fn signals_blocked(&self) -> bool {
74+
cast(self).signals_blocked()
75+
}
76+
77+
fn set_object_name(self: Pin<&mut Self>, name: &QString) {
78+
cast_pin(self).set_object_name(name)
79+
}
80+
81+
fn object_name(&self) -> QString {
82+
cast(self).object_name()
83+
}
84+
85+
fn parent(&self) -> *mut Self {
86+
cast(self).parent() as *mut Self
87+
}
88+
89+
fn set_parent(self: Pin<&mut Self>, parent: &Self) {
90+
unsafe {
91+
cast_pin(self)
92+
.set_parent(cast(parent) as *const QObjectExternal as *mut QObjectExternal);
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)