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.
examples: add example of using a signal on an external object
Related to KDAB#577
- Loading branch information
1 parent
d7ff982
commit 12817aa
Showing
12 changed files
with
314 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#include "external_qobject.h" | ||
|
||
ExternalQObject::ExternalQObject(QObject* parent) | ||
: QObject(parent) | ||
{ | ||
} | ||
|
||
void | ||
ExternalQObject::trigger(::std::uint32_t amount) | ||
{ | ||
for (::std::uint32_t i = 0; i < amount; i++) { | ||
Q_EMIT triggered(); | ||
Q_EMIT triggeredPrivateSignal(QPrivateSignal()); | ||
} | ||
} |
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,25 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtCore/QObject> | ||
|
||
#include <cstdint> | ||
|
||
class ExternalQObject : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit ExternalQObject(QObject* parent = nullptr); | ||
|
||
Q_INVOKABLE void trigger(::std::uint32_t amount); | ||
|
||
Q_SIGNALS: | ||
void triggered(); | ||
void triggeredPrivateSignal(QPrivateSignal); | ||
}; |
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,69 @@ | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
import QtQuick 2.12 | ||
import QtQuick.Controls 2.12 | ||
import QtQuick.Layouts 1.12 | ||
import QtQuick.Window 2.12 | ||
|
||
import com.kdab.cxx_qt.demo 1.0 | ||
import com.kdab.cxx_qt.demo_cpp 1.0 | ||
|
||
Page { | ||
property int amount: 5 | ||
|
||
header: ToolBar { | ||
RowLayout { | ||
anchors.fill: parent | ||
|
||
ToolButton { | ||
text: qsTr("Trigger") | ||
|
||
onClicked: rustExternCxxQt.triggerOnExternal(externalQObject, amountSpinBox.value) | ||
} | ||
|
||
Item { | ||
Layout.fillWidth: true | ||
} | ||
} | ||
} | ||
|
||
ExternalQObject { | ||
id: externalQObject | ||
} | ||
|
||
ExternalCxxQtHelper { | ||
id: rustExternCxxQt | ||
} | ||
|
||
ColumnLayout { | ||
anchors.left: parent.left | ||
anchors.right: parent.right | ||
anchors.verticalCenter: parent.verticalCenter | ||
|
||
Label { | ||
Layout.fillWidth: true | ||
horizontalAlignment: Text.AlignHCenter | ||
text: qsTr("Connecting to signals in external QObject can run closures as normal.") | ||
wrapMode: Text.Wrap | ||
} | ||
|
||
SpinBox { | ||
id: amountSpinBox | ||
Layout.alignment: Qt.AlignHCenter | ||
from: 1 | ||
to: 10 | ||
value: 5 | ||
} | ||
|
||
Label { | ||
Layout.fillWidth: true | ||
horizontalAlignment: Text.AlignHCenter | ||
text: qsTr("Count: %1").arg(rustExternCxxQt.count) | ||
wrapMode: Text.Wrap | ||
} | ||
} | ||
|
||
Component.onCompleted: rustExternCxxQt.connectToExternal(externalQObject) | ||
} |
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,106 @@ | ||
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
//! This example shows how an external QObject with signals can be used | ||
/// A CXX-Qt bridge which shows how an external QObject with signals can be used | ||
// ANCHOR: book_cxx_file_stem | ||
#[cxx_qt::bridge(cxx_file_stem = "externcxxqt")] | ||
pub mod ffi { | ||
unsafe extern "C++Qt" { | ||
include!("external_qobject.h"); | ||
/// ExternalQObject C++ class | ||
type ExternalQObject; | ||
|
||
/// Trigger emitting the signal "amount" times | ||
fn trigger(self: Pin<&mut ExternalQObject>, amount: u32); | ||
|
||
/// Signal that is emitted when trigger is fired | ||
#[qsignal] | ||
fn triggered(self: Pin<&mut ExternalQObject>); | ||
|
||
/// Private signal that is emitted when trigger is fired | ||
#[qsignal] | ||
#[rust_name = "triggered_private_signal"] | ||
pub(self) fn triggeredPrivateSignal(self: Pin<&mut ExternalQObject>); | ||
} | ||
|
||
unsafe extern "RustQt" { | ||
#[qobject] | ||
#[qml_element] | ||
#[qproperty(u32, count)] | ||
#[qproperty(u32, private_count)] | ||
type ExternalCxxQtHelper = super::ExternalCxxQtHelperRust; | ||
|
||
#[qinvokable] | ||
unsafe fn connect_to_external( | ||
self: Pin<&mut ExternalCxxQtHelper>, | ||
external: *mut ExternalQObject, | ||
); | ||
|
||
#[qinvokable] | ||
unsafe fn trigger_on_external( | ||
self: Pin<&mut ExternalCxxQtHelper>, | ||
external: *mut ExternalQObject, | ||
amount: u32, | ||
); | ||
} | ||
|
||
impl cxx_qt::Threading for ExternalCxxQtHelper {} | ||
} | ||
|
||
use core::pin::Pin; | ||
use cxx_qt::Threading; | ||
|
||
/// Test struct | ||
#[derive(Default)] | ||
pub struct ExternalCxxQtHelperRust { | ||
count: u32, | ||
private_count: u32, | ||
} | ||
|
||
impl ffi::ExternalCxxQtHelper { | ||
unsafe fn connect_to_external(self: Pin<&mut Self>, external: *mut ffi::ExternalQObject) { | ||
if let Some(external) = external.as_mut() { | ||
let qt_thread = self.qt_thread(); | ||
let mut pinned_external = Pin::new_unchecked(external); | ||
pinned_external | ||
.as_mut() | ||
.on_triggered(move |_| { | ||
qt_thread | ||
.queue(|mut qobject| { | ||
let new_count = qobject.as_ref().count() + 1; | ||
qobject.as_mut().set_count(new_count); | ||
}) | ||
.unwrap(); | ||
}) | ||
.release(); | ||
|
||
let qt_thread = self.qt_thread(); | ||
pinned_external | ||
.as_mut() | ||
.on_triggered_private_signal(move |_| { | ||
qt_thread | ||
.queue(|mut qobject| { | ||
let new_private_count = qobject.as_ref().private_count() + 1; | ||
qobject.as_mut().set_private_count(new_private_count); | ||
}) | ||
.unwrap(); | ||
}) | ||
.release(); | ||
} | ||
} | ||
|
||
unsafe fn trigger_on_external( | ||
self: Pin<&mut Self>, | ||
external: *mut ffi::ExternalQObject, | ||
amount: u32, | ||
) { | ||
if let Some(external) = external.as_mut() { | ||
let pinned_external = Pin::new_unchecked(external); | ||
pinned_external.trigger(amount); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.