Skip to content

Commit b3a1337

Browse files
committed
WIP: examples: create a multi project to test multiple crates
1 parent 7df274b commit b3a1337

18 files changed

+413
-0
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ members = [
1717
"examples/qml_extension_plugin/plugin/rust",
1818
"examples/qml_features/rust",
1919
"examples/qml_minimal/rust",
20+
"examples/meta_project/rust/main",
21+
"examples/meta_project/rust/sub1",
22+
"examples/meta_project/rust/sub2",
2023

2124
"tests/basic_cxx_only/rust",
2225
"tests/basic_cxx_qt/rust",

examples/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ add_subdirectory(qml_extension_plugin)
99
add_subdirectory(qml_features)
1010
add_subdirectory(qml_minimal)
1111
add_subdirectory(demo_threading)
12+
add_subdirectory(meta_project)

examples/meta_project/CMakeLists.txt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
cmake_minimum_required(VERSION 3.24)
7+
8+
project(example_meta_project)
9+
set(APP_NAME ${PROJECT_NAME})
10+
11+
set(CMAKE_AUTOMOC ON)
12+
set(CMAKE_AUTORCC ON)
13+
set(CMAKE_CXX_STANDARD 17)
14+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15+
16+
if(NOT USE_QT5)
17+
find_package(Qt6 COMPONENTS Core Gui Qml QuickControls2 QmlImportScanner QuickTest Test)
18+
endif()
19+
if(NOT Qt6_FOUND)
20+
find_package(Qt5 5.15 COMPONENTS Core Gui Qml QuickControls2 QmlImportScanner QuickTest Test REQUIRED)
21+
endif()
22+
get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)
23+
24+
find_package(Corrosion QUIET)
25+
if(NOT Corrosion_FOUND)
26+
include(FetchContent)
27+
FetchContent_Declare(
28+
Corrosion
29+
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
30+
GIT_TAG v0.4.1
31+
)
32+
33+
FetchContent_MakeAvailable(Corrosion)
34+
endif()
35+
36+
set(CRATE qml-meta-project)
37+
corrosion_import_crate(MANIFEST_PATH rust/main/Cargo.toml CRATES ${CRATE})
38+
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
39+
corrosion_set_env_vars(${CRATE}
40+
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
41+
"QMAKE=${QMAKE}"
42+
)
43+
add_library(${APP_NAME}_lib INTERFACE)
44+
target_include_directories(${APP_NAME}_lib INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
45+
target_link_libraries(${APP_NAME}_lib INTERFACE
46+
"$<LINK_LIBRARY:WHOLE_ARCHIVE,${CRATE}-static>"
47+
Qt::Core
48+
Qt::Gui
49+
Qt::Qml
50+
Qt::QuickControls2
51+
)
52+
53+
add_executable(${APP_NAME}
54+
cpp/main.cpp
55+
qml/qml.qrc
56+
)
57+
58+
target_include_directories(${APP_NAME} PRIVATE cpp)
59+
target_link_libraries(${APP_NAME} PRIVATE ${APP_NAME}_lib)
60+
qt_import_qml_plugins(${APP_NAME})

examples/meta_project/cpp/main.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
#include <QtGui/QGuiApplication>
8+
#include <QtQml/QQmlApplicationEngine>
9+
10+
int
11+
main(int argc, char* argv[])
12+
{
13+
QGuiApplication app(argc, argv);
14+
15+
QQmlApplicationEngine engine;
16+
17+
const QUrl url(QStringLiteral("qrc:/main.qml"));
18+
QObject::connect(
19+
&engine,
20+
&QQmlApplicationEngine::objectCreated,
21+
&app,
22+
[url](QObject* obj, const QUrl& objUrl) {
23+
if (!obj && url == objUrl)
24+
QCoreApplication::exit(-1);
25+
},
26+
Qt::QueuedConnection);
27+
28+
engine.load(url);
29+
30+
return app.exec();
31+
}

examples/meta_project/qml/main.qml

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import QtQuick.Window 2.12
9+
10+
import com.kdab.cxx_qt.demo 1.0
11+
12+
ApplicationWindow {
13+
id: window
14+
minimumHeight: 480
15+
minimumWidth: 640
16+
title: qsTr("CXX-Qt: Hello World")
17+
visible: true
18+
19+
MainObject {
20+
id: main
21+
}
22+
23+
Sub1Object {
24+
id: sub1
25+
}
26+
27+
Sub2Object {
28+
id: sub2
29+
}
30+
31+
Column {
32+
anchors.fill: parent
33+
anchors.margins: 10
34+
spacing: 10
35+
36+
Label {
37+
text: "Main: " + main.string
38+
}
39+
40+
Label {
41+
text: "Sub1: " + sub1.string
42+
}
43+
44+
Label {
45+
text: "Sub2: " + sub2.string
46+
}
47+
48+
Button {
49+
text: "Increment Number"
50+
51+
onClicked: {
52+
main.increment();
53+
sub1.increment();
54+
sub2.increment();
55+
}
56+
}
57+
}
58+
}

examples/meta_project/qml/qml.qrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE RCC>
2+
<!--
3+
SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
4+
SPDX-FileContributor: Andrew Hayzen <[email protected]>
5+
6+
SPDX-License-Identifier: MIT OR Apache-2.0
7+
-->
8+
<RCC version="1.0">
9+
<qresource prefix="/">
10+
<file>main.qml</file>
11+
</qresource>
12+
</RCC>
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
[package]
6+
name = "qml-meta-project"
7+
version = "0.1.0"
8+
authors = ["Andrew Hayzen <[email protected]>"]
9+
edition = "2021"
10+
license = "MIT OR Apache-2.0"
11+
12+
[lib]
13+
crate-type = ["staticlib"]
14+
15+
[dependencies]
16+
sub1 = { path = "../sub1" }
17+
sub2 = { path = "../sub2" }
18+
19+
cxx.workspace = true
20+
cxx-qt.workspace = true
21+
cxx-qt-lib.workspace = true
22+
23+
[build-dependencies]
24+
cxx-qt-build.workspace = true
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use cxx_qt_build::CxxQtBuilder;
7+
8+
fn main() {
9+
CxxQtBuilder::new().file("src/main_object.rs").build();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
mod main_object;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#[cxx_qt::bridge]
7+
pub mod ffi {
8+
unsafe extern "C++" {
9+
include!("cxx-qt-lib/qstring.h");
10+
type QString = cxx_qt_lib::QString;
11+
}
12+
13+
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
14+
#[derive(Default)]
15+
pub struct MainObject {
16+
#[qproperty]
17+
string: QString,
18+
19+
pub counter: u32,
20+
}
21+
22+
unsafe extern "RustQt" {
23+
#[qinvokable]
24+
fn increment(self: Pin<&mut qobject::MainObject>);
25+
}
26+
}
27+
28+
use core::pin::Pin;
29+
use cxx_qt::CxxQtType;
30+
use cxx_qt_lib::QString;
31+
32+
impl ffi::MainObjectQt {
33+
pub fn increment(self: Pin<&mut Self>) {
34+
let counter = self.rust().counter;
35+
let counter = sub1_object::increment(counter);
36+
let counter = sub2_object::increment(counter);
37+
self.as_mut().rust_mut().counter = counter;
38+
39+
self.set_string(QString::from(&self.rust().counter.to_string()));
40+
}
41+
}
42+
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
[package]
6+
name = "sub1"
7+
version = "0.1.0"
8+
authors = [
9+
"Andrew Hayzen <[email protected]>",
10+
]
11+
edition = "2021"
12+
license = "MIT OR Apache-2.0"
13+
14+
# This will instruct Cargo to create an rlib our main crate is the staticlib
15+
[lib]
16+
crate-type = ["rlib"]
17+
18+
[dependencies]
19+
cxx.workspace = true
20+
cxx-qt.workspace = true
21+
cxx-qt-lib.workspace = true
22+
23+
[build-dependencies]
24+
cxx-qt-build.workspace = true
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use cxx_qt_build::CxxQtBuilder;
7+
8+
fn main() {
9+
CxxQtBuilder::new().file("src/sub1_object.rs").build();
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
mod sub1_object;
7+
8+
pub fn increment(number: u32) -> u32 {
9+
number + 1
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#[cxx_qt::bridge]
7+
pub mod ffi {
8+
unsafe extern "C++" {
9+
include!("cxx-qt-lib/qstring.h");
10+
type QString = cxx_qt_lib::QString;
11+
}
12+
13+
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
14+
#[derive(Default)]
15+
pub struct Sub1Object {
16+
#[qproperty]
17+
string: QString,
18+
19+
pub counter: u32,
20+
}
21+
22+
unsafe extern "RustQt" {
23+
#[qinvokable]
24+
fn increment(self: Pin<&mut qobject::Sub1Object>);
25+
}
26+
}
27+
28+
use core::pin::Pin;
29+
use cxx_qt::CxxQtType;
30+
use cxx_qt_lib::QString;
31+
32+
impl ffi::Sub1ObjectQt {
33+
pub fn increment(mut self: Pin<&mut Self>) {
34+
self.as_mut().rust_mut().counter = crate::increment(self.rust().counter);
35+
36+
let new_string = QString::from(&self.rust().counter.to_string());
37+
self.as_mut().set_string(new_string);
38+
}
39+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
[package]
6+
name = "sub2"
7+
version = "0.1.0"
8+
authors = [
9+
"Andrew Hayzen <[email protected]>",
10+
]
11+
edition = "2021"
12+
license = "MIT OR Apache-2.0"
13+
14+
# This will instruct Cargo to create an rlib our main crate is the staticlib
15+
[lib]
16+
crate-type = ["rlib"]
17+
18+
[dependencies]
19+
cxx.workspace = true
20+
cxx-qt.workspace = true
21+
cxx-qt-lib.workspace = true
22+
23+
[build-dependencies]
24+
cxx-qt-build.workspace = true
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use cxx_qt_build::CxxQtBuilder;
7+
8+
fn main() {
9+
CxxQtBuilder::new().file("src/sub2_object.rs").build();
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
mod sub2_object;
7+
8+
pub fn increment(number: u32) -> u32 {
9+
number + 2
10+
}

0 commit comments

Comments
 (0)