Skip to content

Commit

Permalink
register QML elements from Rust in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Feb 8, 2023
1 parent 1cfe5ca commit c0ead97
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 122 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/github-cxx-qt-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- name: Ubuntu 22.04 (gcc) Qt5
os: ubuntu-22.04
qt_version: 5
# FIXME: valgrind complains about invalid debuginfo. Might be fixed when Ubuntu updates to valgrind 3.20
# https://bugs.kde.org/show_bug.cgi?id=452758
ctest_args: --exclude-regex '^(example_qml_features_test_valgrind|example_qml_minimal_myobject_test_valgrind)$'
qt_qpa_platform: offscreen
compiler_cache_path: /home/runner/.cache/sccache
cargo_dir: ~/.cargo
Expand All @@ -33,6 +36,9 @@ jobs:
- name: Ubuntu 22.04 (gcc) Qt6
os: ubuntu-22.04
qt_version: 6
# FIXME: valgrind complains about invalid debuginfo. Might be fixed when Ubuntu updates to valgrind 3.20
# https://bugs.kde.org/show_bug.cgi?id=452758
ctest_args: --exclude-regex '^(example_qml_features_test_valgrind|example_qml_minimal_myobject_test_valgrind)$'
qt_qpa_platform: offscreen
compiler_cache_path: /home/runner/.cache/sccache
cargo_dir: ~/.cargo
Expand All @@ -59,7 +65,7 @@ jobs:
# once Ubuntu 22.04 is we can move to clang-format-14 everywhere
# for now we need at least clang-format-12 otherwise include reordering fails after clang-format off
# https://github.com/KDAB/cxx-qt/issues/121
ctest_args: --exclude-regex '^(example_qml_extension_plugin_test|reuse_lint|cpp_clang_format|.*valgrind)$'
ctest_args: --exclude-regex '^(example_qml_extension_plugin_test|example_qml_features_test|example_qml_minimal_myobject_test|reuse_lint|cpp_clang_format|.*valgrind)$'
qt_qpa_platform: cocoa
compiler_cache_path: /Users/runner/Library/Caches/Mozilla.sccache
cargo_dir: ~/.cargo
Expand All @@ -69,7 +75,7 @@ jobs:
qt_version: 6
# FIXME: qmltestrunner fails to import QtQuick module
# https://github.com/KDAB/cxx-qt/issues/110
ctest_args: --exclude-regex '^(example_qml_extension_plugin_test|reuse_lint|cpp_clang_format|.*valgrind)$'
ctest_args: --exclude-regex '^(example_qml_extension_plugin_test|example_qml_features_test|example_qml_minimal_myobject_test|reuse_lint|cpp_clang_format|.*valgrind)$'
qt_qpa_platform: cocoa
compiler_cache_path: /Users/runner/Library/Caches/Mozilla.sccache
cargo_dir: ~/.cargo
Expand Down
11 changes: 7 additions & 4 deletions examples/demo_threading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ if(NOT Corrosion_FOUND)
FetchContent_MakeAvailable(Corrosion)
endif()

set(CRATE demo-threading)
set(CRATE cxx-qt-demo-threading)
corrosion_import_crate(MANIFEST_PATH rust/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}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
target_link_libraries(${CRATE} INTERFACE

add_library(${CRATE}_interface INTERFACE)
target_include_directories(${CRATE}_interface INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
target_link_libraries(${CRATE}_interface INTERFACE
"$<LINK_LIBRARY:WHOLE_ARCHIVE,${CRATE}-static>"
Qt::Core
Qt::Gui
Qt::Qml
Expand All @@ -66,5 +69,5 @@ add_executable(${APP_NAME}
qml/qml.qrc
${QML_COMPAT_RESOURCES}
)
target_link_libraries(${APP_NAME} PRIVATE ${CRATE})
target_link_libraries(${APP_NAME} PRIVATE ${CRATE}_interface)
qt_import_qml_plugins(${APP_NAME})
7 changes: 2 additions & 5 deletions examples/demo_threading/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>

#include "cxx-qt-gen/energy_usage.cxxqt.h"
#include "helpers/energyusageproxymodel.h"
#include "helpers/sensor.h"

Expand All @@ -29,11 +28,9 @@ main(int argc, char* argv[])
},
Qt::QueuedConnection);

qmlRegisterType<cxx_qt::energy_usage::EnergyUsage>(
"com.kdab.energy", 1, 0, "EnergyUsage");
qmlRegisterType<EnergyUsageProxyModel>(
"com.kdab.energy", 1, 0, "EnergyUsageProxyModel");
qmlRegisterType<Sensor>("com.kdab.energy", 1, 0, "Sensor");
"com.kdab.energy_cpp", 1, 0, "EnergyUsageProxyModel");
qmlRegisterType<Sensor>("com.kdab.energy_cpp", 1, 0, "Sensor");

engine.load(url);

Expand Down
1 change: 1 addition & 0 deletions examples/demo_threading/qml/MainWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import QtQuick 2.12
import QtQuick.Window 2.12

import com.kdab.energy 1.0
import com.kdab.energy_cpp 1.0

Window {
id: root
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_threading/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# SPDX-License-Identifier: MIT OR Apache-2.0
[package]
name = "demo-threading"
name = "cxx-qt-demo-threading"
version = "0.1.0"
authors = ["Andrew Hayzen <andrew.hayzen@kdab.com>"]
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_threading/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod ffi {
type QString = cxx_qt_lib::QString;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.energy", qml_version = "1.0")]
pub struct EnergyUsage {
/// The average power usage of the connected sensors
#[qproperty]
Expand Down
14 changes: 6 additions & 8 deletions examples/qml_features/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
)
target_include_directories(${CRATE} INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
target_link_libraries(${CRATE} INTERFACE
add_library(${CRATE}_interface INTERFACE)
target_include_directories(${CRATE}_interface INTERFACE "${CXXQT_EXPORT_DIR}/${CRATE}")
target_link_libraries(${CRATE}_interface INTERFACE
"$<LINK_LIBRARY:WHOLE_ARCHIVE,${CRATE}-static>"
Qt::Core
Qt::Gui
Qt::Qml
Qt::QuickControls2
)

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

target_include_directories(${APP_NAME} PRIVATE cpp)
target_link_libraries(${APP_NAME} PRIVATE ${CRATE})
target_link_libraries(${APP_NAME} PRIVATE ${CRATE}_interface)
qt_import_qml_plugins(${APP_NAME})

#
Expand All @@ -84,14 +84,12 @@ copy_qml_test(threading)
copy_qml_test(types)

add_executable(${APP_NAME}_test
cpp/custom_object.h
cpp/custom_object.cpp
cpp/qabstractlistmodelcxx.h
tests/main.cpp
)
target_include_directories(${APP_NAME}_test PRIVATE cpp)
target_link_libraries(${APP_NAME}_test PRIVATE
${CRATE}
${CRATE}_interface
Qt::Test
Qt::QuickTest
)
Expand Down
36 changes: 5 additions & 31 deletions examples/qml_features/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>

#include "cxx-qt-gen/custom_base_class.cxxqt.h"
#include "cxx-qt-gen/multiple_qobjects.cxxqt.h"
#include "cxx-qt-gen/nested_qobjects.cxxqt.h"
#include "cxx-qt-gen/rust_containers.cxxqt.h"
#include "cxx-qt-gen/rust_invokables.cxxqt.h"
#include "cxx-qt-gen/rust_properties.cxxqt.h"
#include "cxx-qt-gen/rust_signals.cxxqt.h"
#include "cxx-qt-gen/serialisation.cxxqt.h"
#include "cxx-qt-gen/threading_website.cxxqt.h"
#include "cxx-qt-gen/types.cxxqt.h"

#include "custom_object.h"

int
Expand All @@ -40,26 +29,11 @@ main(int argc, char* argv[])
Qt::QueuedConnection);

qRegisterMetaType<CustomStruct>("CustomStruct");
qmlRegisterType<CustomObject>("com.kdab.cxx_qt.demo", 1, 0, "CustomObject");
qmlRegisterType<RustContainers>(
"com.kdab.cxx_qt.demo", 1, 0, "RustContainers");
qmlRegisterType<CustomBaseClass>(
"com.kdab.cxx_qt.demo", 1, 0, "CustomBaseClass");
qmlRegisterType<FirstObject>("com.kdab.cxx_qt.demo", 1, 0, "FirstObject");
qmlRegisterType<SecondObject>("com.kdab.cxx_qt.demo", 1, 0, "SecondObject");
qmlRegisterType<RustInvokables>(
"com.kdab.cxx_qt.demo", 1, 0, "RustInvokables");
qmlRegisterType<RustProperties>(
"com.kdab.cxx_qt.demo", 1, 0, "RustProperties");
qmlRegisterType<RustSignals>("com.kdab.cxx_qt.demo", 1, 0, "RustSignals");
qmlRegisterType<Serialisation>("com.kdab.cxx_qt.demo", 1, 0, "Serialisation");
// ANCHOR: book_namespace_register
qmlRegisterType<cxx_qt::website::ThreadingWebsite>(
"com.kdab.cxx_qt.demo", 1, 0, "ThreadingWebsite");
// ANCHOR_END: book_namespace_register
qmlRegisterType<Types>("com.kdab.cxx_qt.demo", 1, 0, "Types");
qmlRegisterType<InnerObject>("com.kdab.cxx_qt.demo", 1, 0, "InnerObject");
qmlRegisterType<OuterObject>("com.kdab.cxx_qt.demo", 1, 0, "OuterObject");
// Note the _cpp at the end of the URI. If qmlRegisterMetatype is used here
// with the same URI as used by the QML elements generated from Rust, none of
// the elements generated by Rust will be available to the QML engine.
qmlRegisterType<CustomObject>(
"com.kdab.cxx_qt.demo_cpp", 1, 0, "CustomObject");

engine.load(url);

Expand Down
1 change: 1 addition & 0 deletions examples/qml_features/qml/pages/TypesPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

import com.kdab.cxx_qt.demo 1.0
import com.kdab.cxx_qt.demo_cpp 1.0

Page {
header: ToolBar {
Expand Down
10 changes: 7 additions & 3 deletions examples/qml_features/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use cxx_qt_build::CxxQtBuilder;

fn main() {
CxxQtBuilder::new()
.cc_builder(|cc| {
cc.include("../cpp");
})
.file("src/containers.rs")
.file("src/custom_base_class.rs")
.file("src/invokables.rs")
Expand All @@ -22,6 +19,13 @@ fn main() {
.file("src/properties.rs")
.file("src/threading.rs")
.file("src/types.rs")
// custom_object.cpp/h need to be handled here rather than CMakeLists.txt,
// otherwise linking cargo tests fails because the symbols from those files are not found.
.cc_builder(|cc| {
cc.include("../cpp");
cc.file("../cpp/custom_object.cpp");
})
.qobject_header("../cpp/custom_object.h")
.build();
}
// ANCHOR_END: book_build_rs
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod ffi {
type QVector_i32 = cxx_qt_lib::QVector<i32>;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
#[derive(Default)]
pub struct RustContainers {
#[qproperty]
Expand Down
6 changes: 5 additions & 1 deletion examples/qml_features/rust/src/custom_base_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ mod ffi {
}

// ANCHOR: book_qobject_base
#[cxx_qt::qobject(base = "QAbstractListModelCXX")]
#[cxx_qt::qobject(
base = "QAbstractListModelCXX",
qml_uri = "com.kdab.cxx_qt.demo",
qml_version = "1.0"
)]
#[derive(Default)]
pub struct CustomBaseClass {
// ANCHOR_END: book_qobject_base
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod ffi {
type QColor = cxx_qt_lib::QColor;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct RustInvokables {
red: f32,
green: f32,
Expand Down
4 changes: 2 additions & 2 deletions examples/qml_features/rust/src/multiple_qobjects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod ffi {
type QUrl = cxx_qt_lib::QUrl;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct FirstObject {
#[qproperty]
counter: i32,
Expand Down Expand Up @@ -51,7 +51,7 @@ mod ffi {
}
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct SecondObject {
#[qproperty]
counter: i32,
Expand Down
4 changes: 2 additions & 2 deletions examples/qml_features/rust/src/nested_qobjects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ mod ffi {
}
// ANCHOR_END: book_extern_block

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
#[derive(Default)]
pub struct InnerObject {
#[qproperty]
counter: i32,
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct OuterObject {
#[qproperty]
inner: *mut CxxInnerObject,
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod ffi {
}

// ANCHOR: book_properties_struct
#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct RustProperties {
#[qproperty]
connected: bool,
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/serialisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod ffi {
type QString = cxx_qt_lib::QString;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct Serialisation {
#[qproperty]
pub number: i32,
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod ffi {
// ANCHOR_END: book_signals_enum

// ANCHOR: book_signals_struct
#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
#[derive(Default)]
pub struct RustSignals;
// ANCHOR: book_signals_struct
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod ffi {
type QUrl = cxx_qt_lib::QUrl;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct ThreadingWebsite {
#[qproperty]
title: QString,
Expand Down
2 changes: 1 addition & 1 deletion examples/qml_features/rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod ffi {
fn qvariantValueOrDefault(variant: &QVariant) -> CustomStruct;
}

#[cxx_qt::qobject]
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
pub struct Types {
#[qproperty]
boolean: bool,
Expand Down
Loading

0 comments on commit c0ead97

Please sign in to comment.