Skip to content

Commit 06fc679

Browse files
ahayzen-kdabBe-ing
authored andcommitted
examples: add an example of a qml_singleton
Related to KDAB#25
1 parent b6894d6 commit 06fc679

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

examples/qml_features/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ copy_qml_test(nested_qobjects)
8080
copy_qml_test(properties)
8181
copy_qml_test(serialisation)
8282
copy_qml_test(signals)
83+
copy_qml_test(singleton)
8384
copy_qml_test(threading)
8485
copy_qml_test(types)
8586

examples/qml_features/qml/main.qml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ ApplicationWindow {
4646

4747
readonly property alias currentItem: drawerView.currentItem
4848

49+
clip: true
4950
height: window.height - header.height
5051
width: Math.min(window.width * 0.66, 200)
5152
y: header.height
@@ -107,6 +108,10 @@ ApplicationWindow {
107108
name: "Nested QObjects"
108109
source: "qrc:/pages/NestedQObjectsPage.qml"
109110
}
111+
ListElement {
112+
name: "Singleton"
113+
source: "qrc:/pages/SingletonPage.qml"
114+
}
110115
}
111116
}
112117
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
2+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
3+
//
4+
// SPDX-License-Identifier: MIT OR Apache-2.0
5+
import QtQuick 2.12
6+
import QtQuick.Controls 2.12
7+
import QtQuick.Layouts 1.12
8+
9+
import com.kdab.cxx_qt.demo 1.0
10+
11+
Page {
12+
header: ToolBar {
13+
RowLayout {
14+
anchors.fill: parent
15+
16+
ToolButton {
17+
text: qsTr("Increment")
18+
19+
onClicked: RustSingleton.increment()
20+
}
21+
22+
Item {
23+
Layout.fillWidth: true
24+
}
25+
}
26+
}
27+
28+
ColumnLayout {
29+
anchors.left: parent.left
30+
anchors.right: parent.right
31+
anchors.verticalCenter: parent.verticalCenter
32+
33+
Label {
34+
Layout.fillWidth: true
35+
horizontalAlignment: Text.AlignHCenter
36+
text: qsTr("Singletons can be used from Rust to store state across the session of the application as normal.")
37+
wrapMode: Text.Wrap
38+
}
39+
40+
Label {
41+
Layout.fillWidth: true
42+
horizontalAlignment: Text.AlignHCenter
43+
text: qsTr("In this demo increment this value will persist, even after switching pages.")
44+
wrapMode: Text.Wrap
45+
}
46+
47+
Label {
48+
Layout.fillWidth: true
49+
horizontalAlignment: Text.AlignHCenter
50+
text: RustSingleton.persistentValue
51+
wrapMode: Text.Wrap
52+
}
53+
}
54+
}

examples/qml_features/qml/qml.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SPDX-License-Identifier: MIT OR Apache-2.0
1717
<file>pages/PropertiesPage.qml</file>
1818
<file>pages/SerialisationPage.qml</file>
1919
<file>pages/SignalsPage.qml</file>
20+
<file>pages/SingletonPage.qml</file>
2021
<file>pages/ThreadingPage.qml</file>
2122
<file>pages/TypesPage.qml</file>
2223
</qresource>

examples/qml_features/rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616
.file("src/nested_qobjects.rs")
1717
.file("src/serialisation.rs")
1818
.file("src/signals.rs")
19+
.file("src/singleton.rs")
1920
.file("src/properties.rs")
2021
.file("src/threading.rs")
2122
.file("src/types.rs")

examples/qml_features/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ mod nested_qobjects;
1212
mod properties;
1313
mod serialisation;
1414
mod signals;
15+
mod singleton;
1516
mod threading;
1617
mod types;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
2+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
3+
//
4+
// SPDX-License-Identifier: MIT OR Apache-2.0
5+
6+
// ANCHOR: book_macro_code
7+
#[cxx_qt::bridge(cxx_file_stem = "rust_singleton")]
8+
pub mod ffi {
9+
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0", qml_singleton)]
10+
#[derive(Default)]
11+
pub struct RustSingleton {
12+
#[qproperty]
13+
persistent_value: i32,
14+
}
15+
16+
impl qobject::RustSingleton {
17+
#[qinvokable]
18+
pub fn increment(self: Pin<&mut Self>) {
19+
let new_value = self.persistent_value() + 1;
20+
self.set_persistent_value(new_value);
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
2+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
3+
//
4+
// SPDX-License-Identifier: MIT OR Apache-2.0
5+
import QtQuick 2.12
6+
import QtTest 1.12
7+
8+
import com.kdab.cxx_qt.demo 1.0
9+
10+
TestCase {
11+
name: "SingletonTests"
12+
13+
function test_increment() {
14+
compare(RustSingleton.persistentValue, 0);
15+
RustSingleton.increment();
16+
compare(RustSingleton.persistentValue, 1);
17+
}
18+
}

0 commit comments

Comments
 (0)