Skip to content

Commit 0626e9a

Browse files
QML Features: Add Constructor to Invokables
1 parent 6794073 commit 0626e9a

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

examples/qml_features/cpp/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
#include <QtQml/QQmlApplicationEngine>
1010

1111
#include "custom_object.h"
12+
#include "cxx-qt-gen/rust_invokables.cxxqt.h"
1213

1314
int
1415
main(int argc, char* argv[])
1516
{
1617
QGuiApplication app(argc, argv);
1718

19+
RustInvokables invokables(0.f, 0.f, 1.f, nullptr);
20+
1821
QQmlApplicationEngine engine;
1922

2023
const QUrl url(QStringLiteral("qrc:/main.qml"));
@@ -35,6 +38,9 @@ main(int argc, char* argv[])
3538
qmlRegisterType<CustomObject>(
3639
"com.kdab.cxx_qt.demo_cpp", 1, 0, "CustomObject");
3740

41+
qmlRegisterSingletonInstance(
42+
"com.kdab.cxx_qt.demo_cpp", 1, 0, "Invokables", &invokables);
43+
3844
engine.load(url);
3945

4046
return app.exec();

examples/qml_features/qml/pages/InvokablesPage.qml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import QtQuick 2.12
66
import QtQuick.Controls 2.12
77
import QtQuick.Layouts 1.12
88

9-
import com.kdab.cxx_qt.demo 1.0
9+
import com.kdab.cxx_qt.demo_cpp 1.0
1010

1111
Page {
1212
background: Rectangle {
@@ -36,7 +36,7 @@ Page {
3636

3737
onClicked: {
3838
timerSync.running = false;
39-
rustInvokables.reset();
39+
Invokables.reset();
4040
privateState.load();
4141
}
4242
}
@@ -47,18 +47,14 @@ Page {
4747
}
4848
}
4949

50-
RustInvokables {
51-
id: rustInvokables
52-
}
53-
5450
QtObject {
5551
id: privateState
5652

5753
property color color
5854
property bool loaded: false
5955

6056
function load() {
61-
color = rustInvokables.loadColor();
57+
color = Invokables.loadColor();
6258
}
6359

6460
Component.onCompleted: {
@@ -82,7 +78,7 @@ Page {
8278
if (!privateState.loaded) {
8379
return;
8480
}
85-
rustInvokables.storeColor(sliderRed.value, sliderGreen.value, sliderBlue.value);
81+
Invokables.storeColor(sliderRed.value, sliderGreen.value, sliderBlue.value);
8682
}
8783

8884
Slider {

examples/qml_features/rust/src/invokables.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,38 @@ pub mod ffi {
1313
include!("cxx-qt-lib/qcolor.h");
1414
/// QColor from cxx_qt_lib
1515
type QColor = cxx_qt_lib::QColor;
16+
17+
include!(<QObject>);
18+
/// QObject from Qt
19+
type QObject;
1620
}
21+
impl UniquePtr<QObject> {}
1722

1823
/// A QObject which has Q_INVOKABLEs
19-
#[cxx_qt::qobject(qml_uri = "com.kdab.cxx_qt.demo", qml_version = "1.0")]
24+
#[cxx_qt::qobject]
2025
pub struct RustInvokables {
2126
red: f32,
2227
green: f32,
2328
blue: f32,
2429
}
2530

26-
impl Default for RustInvokables {
27-
fn default() -> Self {
28-
Self {
29-
red: 0.0,
30-
green: 0.4667,
31-
blue: 0.7843,
32-
}
31+
impl cxx_qt::Constructor<(f32, f32, f32, UniquePtr<QObject>)> for qobject::RustInvokables {
32+
type BaseArguments = (*mut QObject,);
33+
type NewArguments = (f32, f32, f32);
34+
type InitializeArguments = ();
35+
36+
fn route_arguments(
37+
(r, g, b, parent): (f32, f32, f32, UniquePtr<QObject>),
38+
) -> (
39+
Self::NewArguments,
40+
Self::BaseArguments,
41+
Self::InitializeArguments,
42+
) {
43+
((r, g, b), (parent.into_raw(),), ())
44+
}
45+
46+
fn new((red, green, blue): Self::NewArguments) -> RustInvokables {
47+
RustInvokables { red, green, blue }
3348
}
3449
}
3550

0 commit comments

Comments
 (0)