Skip to content

Commit

Permalink
QML Features: Add Constructor to Invokables
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMatthesKDAB committed Jun 2, 2023
1 parent 83c4324 commit 8c1978a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
6 changes: 6 additions & 0 deletions examples/qml_features/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
#include <QtQml/QQmlApplicationEngine>

#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"));
Expand All @@ -35,6 +38,9 @@ main(int argc, char* argv[])
qmlRegisterType<CustomObject>(
"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();
Expand Down
12 changes: 4 additions & 8 deletions examples/qml_features/qml/pages/InvokablesPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -36,7 +36,7 @@ Page {

onClicked: {
timerSync.running = false;
rustInvokables.reset();
Invokables.reset();
privateState.load();
}
}
Expand All @@ -47,18 +47,14 @@ Page {
}
}

RustInvokables {
id: rustInvokables
}

QtObject {
id: privateState

property color color
property bool loaded: false

function load() {
color = rustInvokables.loadColor();
color = Invokables.loadColor();
}

Component.onCompleted: {
Expand All @@ -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 {
Expand Down
31 changes: 23 additions & 8 deletions examples/qml_features/rust/src/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
/// QObject from Qt
type QObject;
}
impl UniquePtr<QObject> {}

/// 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<QObject>)> 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<QObject>),
) -> (
Self::NewArguments,
Self::BaseArguments,
Self::InitializeArguments,
) {
((r, g, b), (parent.into_raw(),), ())
}

fn new((red, green, blue): Self::NewArguments) -> RustInvokables {
RustInvokables { red, green, blue }
}
}

Expand Down

0 comments on commit 8c1978a

Please sign in to comment.