-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmultiple_qobjects.rs
139 lines (118 loc) · 3.61 KB
/
multiple_qobjects.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//! This example shows how multiple QObjects can be defined in one module
/// A CXX-Qt bridge which shows multiple QObjects can be defined in one module
#[cxx_qt::bridge]
pub mod qobject {
unsafe extern "C++" {
include!("cxx-qt-lib/qcolor.h");
/// QColor from cxx_qt_lib
type QColor = cxx_qt_lib::QColor;
include!("cxx-qt-lib/qurl.h");
/// QUrl from cxx_qt_lib
type QUrl = cxx_qt_lib::QUrl;
}
extern "RustQt" {
#[qobject]
#[qml_element]
#[qproperty(i32, counter)]
#[qproperty(QColor, color)]
type FirstObject = super::FirstObjectRust;
}
// Enabling threading on the qobject
impl cxx_qt::Threading for FirstObject {}
unsafe extern "RustQt" {
/// Accepted Q_SIGNAL
#[qsignal]
fn accepted(self: Pin<&mut FirstObject>);
/// Rejected Q_SIGNAL
#[qsignal]
fn rejected(self: Pin<&mut FirstObject>);
}
unsafe extern "RustQt" {
/// A Q_INVOKABLE on the first QObject which increments a counter
#[qinvokable]
fn increment(self: Pin<&mut FirstObject>);
}
extern "RustQt" {
#[qobject]
#[qml_element]
#[qproperty(i32, counter)]
#[qproperty(QUrl, url)]
type SecondObject = super::SecondObjectRust;
}
// Enabling threading on the qobject
impl cxx_qt::Threading for SecondObject {}
unsafe extern "RustQt" {
/// Accepted Q_SIGNAL
#[qsignal]
fn accepted(self: Pin<&mut SecondObject>);
/// Rejected Q_SIGNAL
#[qsignal]
fn rejected(self: Pin<&mut SecondObject>);
}
unsafe extern "RustQt" {
/// A Q_INVOKABLE on the second QObject which increments a counter
#[qinvokable]
fn increment(self: Pin<&mut SecondObject>);
}
}
use core::pin::Pin;
use cxx_qt_lib::{QColor, QUrl};
/// The first QObject
pub struct FirstObjectRust {
counter: i32,
color: QColor,
}
impl Default for FirstObjectRust {
fn default() -> Self {
Self {
counter: 10,
color: QColor::from_rgb(0, 0, 255),
}
}
}
impl qobject::FirstObject {
/// A Q_INVOKABLE on the first QObject which increments a counter
pub fn increment(mut self: Pin<&mut Self>) {
let new_value = self.as_ref().counter() + 1;
self.as_mut().set_counter(new_value);
if new_value % 2 == 0 {
self.as_mut().set_color(QColor::from_rgb(0, 0, 255));
self.accepted();
} else {
self.as_mut().set_color(QColor::from_rgb(255, 0, 0));
self.rejected();
}
}
}
/// The second QObject
pub struct SecondObjectRust {
counter: i32,
url: QUrl,
}
impl Default for SecondObjectRust {
fn default() -> Self {
Self {
counter: 100,
url: QUrl::from("https://github.com/kdab/cxx-qt"),
}
}
}
impl qobject::SecondObject {
/// A Q_INVOKABLE on the second QObject which increments a counter
pub fn increment(mut self: Pin<&mut Self>) {
let new_value = self.as_ref().counter() + 1;
self.as_mut().set_counter(new_value);
if new_value % 5 == 0 {
self.as_mut()
.set_url(QUrl::from("https://github.com/kdab/cxx-qt"));
self.accepted();
} else {
self.as_mut().set_url(QUrl::from("https://kdab.com"));
self.rejected();
}
}
}