Skip to content

Commit e0c231e

Browse files
Add qobject extension trait
- cxx-qt-lib trait for additional qobject wrappers - adds blockSignals - adds signalsBlocked - adds setObjectName - adds objectName
1 parent 3c6dbf1 commit e0c231e

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
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
@@ -44,6 +44,9 @@ pub use cxx_qt::{QMetaObjectConnection, QMetaObjectConnectionGuard};
4444
mod qmodelindex;
4545
pub use qmodelindex::QModelIndex;
4646

47+
mod qobject;
48+
pub use qobject::QObjectExt;
49+
4750
mod qpersistentmodelindex;
4851
pub use qpersistentmodelindex::QPersistentModelIndex;
4952

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

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
31+
32+
use crate::core::qobject::ffi::QString;
33+
use ffi::QObjectExternal;
34+
35+
pub trait QObjectExt {
36+
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool;
37+
38+
fn signals_blocked(&self) -> bool;
39+
40+
fn set_object_name(self: Pin<&mut Self>, name: &QString);
41+
42+
fn object_name(&self) -> QString;
43+
}
44+
45+
fn cast_pin(obj: Pin<&mut QObject>) -> Pin<&mut QObjectExternal> {
46+
unsafe {
47+
let mut_ptr = obj.get_unchecked_mut() as *mut QObject as *mut QObjectExternal;
48+
Pin::new_unchecked(&mut *mut_ptr)
49+
}
50+
}
51+
52+
fn cast(obj: &QObject) -> &QObjectExternal {
53+
unsafe {
54+
let ptr = obj as *const QObject as *const QObjectExternal;
55+
&*ptr
56+
}
57+
}
58+
59+
impl QObjectExt for QObject {
60+
fn block_signals(self: Pin<&mut Self>, block: bool) -> bool {
61+
cast_pin(self).block_signals(block)
62+
}
63+
64+
fn signals_blocked(self: &Self) -> bool {
65+
cast(self).signals_blocked()
66+
}
67+
68+
fn set_object_name(self: Pin<&mut Self>, name: &QString) {
69+
cast_pin(self).set_object_name(name)
70+
}
71+
72+
fn object_name(self: &Self) -> QString {
73+
cast(self).object_name()
74+
}
75+
}

0 commit comments

Comments
 (0)