forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx-qt-gen: add support for nested objects (pointer types)
Closes KDAB#299
- Loading branch information
1 parent
52f593a
commit 6afdd77
Showing
11 changed files
with
237 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
import QtQuick 2.12 | ||
import QtQuick.Controls 2.12 | ||
import QtQuick.Layouts 1.12 | ||
|
||
import com.kdab.cxx_qt.demo 1.0 | ||
|
||
Page { | ||
header: ToolBar { | ||
RowLayout { | ||
anchors.fill: parent | ||
|
||
ToolButton { | ||
text: qsTr("Increment") | ||
|
||
onClicked: outerObject.inner.counter += 1 | ||
} | ||
|
||
|
||
ToolButton { | ||
text: qsTr("Reset") | ||
|
||
onClicked: outerObject.reset() | ||
} | ||
|
||
ToolButton { | ||
text: qsTr("Print") | ||
|
||
onClicked: outerObject.invokable(innerObject) | ||
} | ||
|
||
Item { | ||
Layout.fillWidth: true | ||
} | ||
} | ||
} | ||
|
||
InnerObject { | ||
id: innerObject | ||
counter: 10 | ||
} | ||
|
||
OuterObject { | ||
id: outerObject | ||
inner: innerObject | ||
|
||
onCalled: (inner) => console.warn("Signal called, inner value: ", inner.counter) | ||
} | ||
|
||
Label { | ||
anchors.centerIn: parent | ||
text: innerObject.counter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
// ANCHOR: book_macro_code | ||
#[cxx_qt::bridge(cxx_file_stem = "nested_qobjects")] | ||
mod ffi { | ||
// ANCHOR: book_extern_block | ||
unsafe extern "C++" { | ||
#[cxx_name = "InnerObject"] | ||
type InnerObjectPtr = super::qobject::InnerObject; | ||
} | ||
// ANCHOR_END: book_extern_block | ||
|
||
#[cxx_qt::qobject] | ||
#[derive(Default)] | ||
pub struct InnerObject { | ||
#[qproperty] | ||
counter: i32, | ||
} | ||
|
||
#[cxx_qt::qobject] | ||
pub struct OuterObject { | ||
#[qproperty] | ||
inner: *mut InnerObjectPtr, | ||
} | ||
|
||
impl Default for OuterObject { | ||
fn default() -> Self { | ||
Self { | ||
inner: std::ptr::null_mut(), | ||
} | ||
} | ||
} | ||
|
||
#[cxx_qt::qsignals(OuterObject)] | ||
pub enum OuterSignals { | ||
Called { inner: *mut InnerObjectPtr }, | ||
} | ||
|
||
impl qobject::OuterObject { | ||
#[qinvokable] | ||
pub fn invokable(self: Pin<&mut Self>, inner: *mut InnerObjectPtr) { | ||
if let Some(inner) = unsafe { inner.as_ref() } { | ||
println!("Inner object: {}", inner.counter()); | ||
} | ||
|
||
self.emit(OuterSignals::Called { inner }); | ||
} | ||
|
||
#[qinvokable] | ||
pub fn reset(self: Pin<&mut Self>) { | ||
// We need to convert the *mut T to a Pin<&mut T> so that we can reach the methods | ||
if let Some(inner) = unsafe { self.inner().as_mut() } { | ||
let pinned_inner = unsafe { Pin::new_unchecked(inner) }; | ||
// Now pinned inner can be used as normal | ||
pinned_inner.set_counter(10); | ||
} | ||
|
||
// Retrieve *mut T | ||
let inner = *self.inner(); | ||
self.emit(OuterSignals::Called { inner }); | ||
} | ||
} | ||
} | ||
// ANCHOR_END: book_macro_code |