diff --git a/examples/qml_features/cpp/main.cpp b/examples/qml_features/cpp/main.cpp index fb69218fc..fab53cd64 100644 --- a/examples/qml_features/cpp/main.cpp +++ b/examples/qml_features/cpp/main.cpp @@ -9,12 +9,15 @@ #include #include "custom_object.h" +#include "cxx-qt-gen/rust_invokables.cxxqt.h" int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); + RustInvokables invokables(0.f, 0.f, 1.f, nullptr); + QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); @@ -35,6 +38,9 @@ main(int argc, char* argv[]) qmlRegisterType( "com.kdab.cxx_qt.demo_cpp", 1, 0, "CustomObject"); + qmlRegisterSingletonInstance( + "com.kdab.cxx_qt.demo_cpp", 1, 0, "Invokables", &invokables); + engine.load(url); return app.exec(); diff --git a/examples/qml_features/qml/pages/InvokablesPage.qml b/examples/qml_features/qml/pages/InvokablesPage.qml index 5d63f83c7..3ad1b31b6 100644 --- a/examples/qml_features/qml/pages/InvokablesPage.qml +++ b/examples/qml_features/qml/pages/InvokablesPage.qml @@ -6,7 +6,7 @@ import QtQuick 2.12 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 { background: Rectangle { @@ -36,7 +36,7 @@ Page { onClicked: { timerSync.running = false; - rustInvokables.reset(); + Invokables.reset(); privateState.load(); } } @@ -47,10 +47,6 @@ Page { } } - RustInvokables { - id: rustInvokables - } - QtObject { id: privateState @@ -58,7 +54,7 @@ Page { property bool loaded: false function load() { - color = rustInvokables.loadColor(); + color = Invokables.loadColor(); } Component.onCompleted: { @@ -82,7 +78,7 @@ Page { if (!privateState.loaded) { return; } - rustInvokables.storeColor(sliderRed.value, sliderGreen.value, sliderBlue.value); + Invokables.storeColor(sliderRed.value, sliderGreen.value, sliderBlue.value); } Slider { diff --git a/examples/qml_features/rust/src/invokables.rs b/examples/qml_features/rust/src/invokables.rs index 201135131..972ae9097 100644 --- a/examples/qml_features/rust/src/invokables.rs +++ b/examples/qml_features/rust/src/invokables.rs @@ -13,23 +13,38 @@ pub mod ffi { include!("cxx-qt-lib/qcolor.h"); /// QColor from cxx_qt_lib type QColor = cxx_qt_lib::QColor; + + include!(); + /// QObject from Qt + type QObject; } + impl UniquePtr {} /// A QObject which has Q_INVOKABLEs - #[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")] + #[cxx_qt::qobject] pub struct RustInvokables { red: f32, green: f32, blue: f32, } - impl Default for RustInvokables { - fn default() -> Self { - Self { - red: 0.0, - green: 0.4667, - blue: 0.7843, - } + impl cxx_qt::Constructor<(f32, f32, f32, UniquePtr)> for qobject::RustInvokables { + type BaseArguments = (*mut QObject,); + type NewArguments = (f32, f32, f32); + type InitializeArguments = (); + + fn route_arguments( + (r, g, b, parent): (f32, f32, f32, UniquePtr), + ) -> ( + Self::NewArguments, + Self::BaseArguments, + Self::InitializeArguments, + ) { + ((r, g, b), (parent.into_raw(),), ()) + } + + fn new((red, green, blue): Self::NewArguments) -> RustInvokables { + RustInvokables { red, green, blue } } }