Skip to content

Commit

Permalink
WIP: examples: create a multi project to test multiple crates
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Aug 16, 2023
1 parent 288c079 commit 5979009
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ members = [
"examples/demo_threading/rust",
"examples/qml_features/rust",
"examples/qml_minimal/rust",
"examples/meta_project/rust/main",
"examples/meta_project/rust/sub1",
"examples/meta_project/rust/sub2",

"tests/basic_cxx_only/rust",
"tests/basic_cxx_qt/rust",
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ add_subdirectory(cargo_without_cmake)
add_subdirectory(qml_features)
add_subdirectory(qml_minimal)
add_subdirectory(demo_threading)
add_subdirectory(meta_project)
60 changes: 60 additions & 0 deletions examples/meta_project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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

cmake_minimum_required(VERSION 3.24)

project(example_meta_project)
set(APP_NAME ${PROJECT_NAME})

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT USE_QT5)
find_package(Qt6 COMPONENTS Core Gui Qml QuickControls2 QmlImportScanner QuickTest Test)
endif()
if(NOT Qt6_FOUND)
find_package(Qt5 5.15 COMPONENTS Core Gui Qml QuickControls2 QmlImportScanner QuickTest Test REQUIRED)
endif()
get_target_property(QMAKE Qt::qmake IMPORTED_LOCATION)

find_package(Corrosion QUIET)
if(NOT Corrosion_FOUND)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG v0.4.1
)

FetchContent_MakeAvailable(Corrosion)
endif()

set(CRATE qml-meta-project)
corrosion_import_crate(MANIFEST_PATH rust/main/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
add_library(${APP_NAME}_lib INTERFACE)
target_include_directories(${APP_NAME}_lib INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
target_link_libraries(${APP_NAME}_lib INTERFACE
"$<LINK_LIBRARY:WHOLE_ARCHIVE,${CRATE}-static>"
Qt::Core
Qt::Gui
Qt::Qml
Qt::QuickControls2
)

add_executable(${APP_NAME}
cpp/main.cpp
qml/qml.qrc
)

target_include_directories(${APP_NAME} PRIVATE cpp)
target_link_libraries(${APP_NAME} PRIVATE ${APP_NAME}_lib)
qt_import_qml_plugins(${APP_NAME})
31 changes: 31 additions & 0 deletions examples/meta_project/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>

int
main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;

const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreated,
&app,
[url](QObject* obj, const QUrl& objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
},
Qt::QueuedConnection);

engine.load(url);

return app.exec();
}
60 changes: 60 additions & 0 deletions examples/meta_project/qml/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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.sub1 1.0
import com.kdab.cxx_qt.demo.sub2 1.0

ApplicationWindow {
id: window
minimumHeight: 480
minimumWidth: 640
title: qsTr("CXX-Qt: Hello World")
visible: true

MainObject {
id: main
}

Sub1Object {
id: sub1
}

Sub2Object {
id: sub2
}

Column {
anchors.fill: parent
anchors.margins: 10
spacing: 10

Label {
text: "Main: " + main.string
}

Label {
text: "Sub1: " + sub1.string
}

Label {
text: "Sub2: " + sub2.string
}

Button {
text: "Increment Number"

onClicked: {
main.increment();
sub1.increment();
sub2.increment();
}
}
}
}
12 changes: 12 additions & 0 deletions examples/meta_project/qml/qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE RCC>
<!--
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
-->
<RCC version="1.0">
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
24 changes: 24 additions & 0 deletions examples/meta_project/rust/main/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
[package]
name = "qml-meta-project"
version = "0.1.0"
authors = ["Andrew Hayzen <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"

[lib]
crate-type = ["staticlib"]

[dependencies]
sub1 = { path = "../sub1" }
sub2 = { path = "../sub2" }

cxx.workspace = true
cxx-qt.workspace = true
cxx-qt-lib.workspace = true

[build-dependencies]
cxx-qt-build.workspace = true
16 changes: 16 additions & 0 deletions examples/meta_project/rust/main/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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

use cxx_qt_build::CxxQtBuilder;

fn main() {
CxxQtBuilder::new()
.qml_module(QmlModule {
uri: "com.kdab.cxx_qt.demo",
rust_files: &["src/main_object.rs"],
..Default::default()
})
.build();
}
6 changes: 6 additions & 0 deletions examples/meta_project/rust/main/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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

mod main_object;
44 changes: 44 additions & 0 deletions examples/meta_project/rust/main/src/main_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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

#[cxx_qt::bridge]
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}

unsafe extern "RustQt" {
#[qobject]
#[qproperty(QString, string)]
type MainObject = super::MainObjectRust;

#[qinvokable]
fn increment(self: Pin<&mut MainObject>);
}
}

use core::pin::Pin;
use cxx_qt::CxxQtType;
use cxx_qt_lib::QString;

#[derive(Default)]
pub struct MainObjectRust {
string: QString,

pub counter: u32,
}

impl qobject::MainObject {
pub fn increment(mut self: Pin<&mut Self>) {
let counter = self.rust().counter;
let counter = sub1::increment(counter);
let counter = sub2::increment(counter);
self.as_mut().rust_mut().counter = counter;

let new_string = QString::from(&self.rust().counter.to_string());
self.as_mut().set_string(new_string);
}
}
24 changes: 24 additions & 0 deletions examples/meta_project/rust/sub1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
[package]
name = "sub1"
version = "0.1.0"
authors = [
"Andrew Hayzen <[email protected]>",
]
edition = "2021"
license = "MIT OR Apache-2.0"

# This will instruct Cargo to create an rlib our main crate is the staticlib
[lib]
crate-type = ["rlib"]

[dependencies]
cxx.workspace = true
cxx-qt.workspace = true
cxx-qt-lib.workspace = true

[build-dependencies]
cxx-qt-build.workspace = true
16 changes: 16 additions & 0 deletions examples/meta_project/rust/sub1/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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

use cxx_qt_build::{CxxQtBuilder, QmlModule};

fn main() {
CxxQtBuilder::new()
.qml_module(QmlModule {
uri: "com.kdab.cxx_qt.demo.sub1",
rust_files: &["src/sub1_object.rs"],
..Default::default()
})
.build();
}
16 changes: 16 additions & 0 deletions examples/meta_project/rust/sub1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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

// We need to enable packed bundled libs to allow for +bundle and +whole-archive
//
// Note that this is a nightly feature
// https://github.com/rust-lang/rust/issues/108081
#![feature(packed_bundled_libs)]

mod sub1_object;

pub fn increment(number: u32) -> u32 {
number + 1
}
41 changes: 41 additions & 0 deletions examples/meta_project/rust/sub1/src/sub1_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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

#[cxx_qt::bridge]
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}

unsafe extern "RustQt" {
#[qobject]
#[qproperty(QString, string)]
type Sub1Object = super::Sub1ObjectRust;

#[qinvokable]
fn increment(self: Pin<&mut Sub1Object>);
}
}

use core::pin::Pin;
use cxx_qt::CxxQtType;
use cxx_qt_lib::QString;

#[derive(Default)]
pub struct Sub1ObjectRust {
string: QString,

pub counter: u32,
}

impl qobject::Sub1Object {
pub fn increment(mut self: Pin<&mut Self>) {
self.as_mut().rust_mut().counter = crate::increment(self.rust().counter);

let new_string = QString::from(&self.rust().counter.to_string());
self.as_mut().set_string(new_string);
}
}
Loading

0 comments on commit 5979009

Please sign in to comment.