Skip to content

Commit 8e41afd

Browse files
LeonMatthesKDABahayzen-kdab
authored andcommitted
QML Features: Add constructor to nested qobjects
1 parent c1ff831 commit 8e41afd

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

examples/qml_features/qml/pages/NestedQObjectsPage.qml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ Page {
4141
InnerObject {
4242
id: innerObject
4343
counter: 10
44+
45+
onCalled: () => console.warn("Inner signal called")
4446
}
4547

4648
OuterObject {
4749
id: outerObject
4850
inner: innerObject
4951

5052
onCalled: (inner) => console.warn("Signal called, inner value: ", inner.counter)
51-
52-
Component.onCompleted: initialise()
5353
}
5454

5555
Label {

examples/qml_features/rust/src/nested_qobjects.rs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ pub mod ffi {
5353
}
5454

5555
unsafe extern "RustQt" {
56-
/// Initialise the QObject, creating a connection from one signal to another
57-
#[qinvokable]
58-
fn initialise(self: Pin<&mut qobject::OuterObject>);
59-
6056
/// Print the count of the given inner QObject
6157
//
6258
// This method needs to be unsafe otherwise clippy complains that the
@@ -68,30 +64,15 @@ pub mod ffi {
6864
#[qinvokable]
6965
fn reset(self: Pin<&mut qobject::OuterObject>);
7066
}
67+
68+
impl cxx_qt::Constructor<()> for qobject::OuterObject {}
7169
}
7270

7371
use core::pin::Pin;
7472

7573
// TODO: this will change to qobject::OuterObject once
7674
// https://github.com/KDAB/cxx-qt/issues/559 is done
7775
impl ffi::OuterObjectQt {
78-
/// Initialise the QObject, creating a connection from one signal to another
79-
fn initialise(self: Pin<&mut Self>) {
80-
// Example of connecting a signal from one QObject to another QObject
81-
// this causes OuterObject::Called to trigger InnerObject::Called
82-
self.on_called(|qobject, obj| {
83-
// We need to convert the *mut T to a Pin<&mut T> so that we can reach the methods
84-
if let Some(inner) = unsafe { qobject.inner().as_mut() } {
85-
let pinned_inner = unsafe { Pin::new_unchecked(inner) };
86-
// Now pinned inner can be used as normal
87-
unsafe {
88-
pinned_inner.called(obj);
89-
}
90-
}
91-
})
92-
.release();
93-
}
94-
9576
/// Print the count of the given inner QObject
9677
//
9778
// This method needs to be unsafe otherwise clippy complains that the
@@ -120,4 +101,42 @@ impl ffi::OuterObjectQt {
120101
unsafe { self.called(inner) };
121102
}
122103
}
104+
105+
impl cxx_qt::Constructor<()> for qobject::OuterObject {
106+
type InitializeArguments = ();
107+
type NewArguments = ();
108+
type BaseArguments = ();
109+
110+
fn route_arguments(
111+
_: (),
112+
) -> (
113+
Self::NewArguments,
114+
Self::BaseArguments,
115+
Self::InitializeArguments,
116+
) {
117+
((), (), ())
118+
}
119+
120+
fn new(_: Self::NewArguments) -> <Self as cxx_qt::CxxQtType>::Rust {
121+
Default::default()
122+
}
123+
124+
/// Initialise the QObject, creating a connection from one signal to another
125+
fn initialize(self: core::pin::Pin<&mut Self>, _: Self::InitializeArguments) {
126+
// Example of connecting a signal from one QObject to another QObject
127+
// this causes OuterObject::Called to trigger InnerObject::Called
128+
self.on_called(|qobject, obj| {
129+
// We need to convert the *mut T to a Pin<&mut T> so that we can reach the methods
130+
if let Some(inner) = unsafe { qobject.inner().as_mut() } {
131+
let pinned_inner = unsafe { Pin::new_unchecked(inner) };
132+
// Now pinned inner can be used as normal
133+
unsafe {
134+
pinned_inner.called(obj);
135+
}
136+
}
137+
})
138+
.release();
139+
}
140+
}
141+
123142
// ANCHOR_END: book_macro_code

examples/qml_features/tests/tst_nested_qobjects.qml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ TestCase {
1919

2020
Component {
2121
id: componentOuterObject
22-
OuterObject {
23-
Component.onCompleted: initialise()
24-
}
22+
OuterObject { }
2523
}
2624

2725
Component {

0 commit comments

Comments
 (0)