-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: examples: create a multi project to test multiple crates
- Loading branch information
1 parent
288c079
commit 5979009
Showing
18 changed files
with
451 additions
and
0 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,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}) |
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,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(); | ||
} |
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,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(); | ||
} | ||
} | ||
} | ||
} |
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,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> |
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,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 |
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,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(); | ||
} |
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,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; |
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,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); | ||
} | ||
} |
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,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 |
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,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(); | ||
} |
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,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 | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.