I am pretty new to Rust and simply found the idea intriguing and adapt to an existing QML frontend (this one more precisely). The first steps went pretty smooth starting from the qmetaobject example and I was quickly able to get some visuals rendered on screen. :+1:
However I got a little stuck with how to register a QObject as a root context property to interface with the application backend. A little (functional C++) snippet should illustrate this.
In the C++ application those (global) objects are then accessible in QML.
Text { text: "Obj1 name:" + Obj1.name }
I could not find an example on how to formulate something like this with qmetaobject-rs. Something like qml_register_singleton_instance(…) would also be fine. The question is more like how to correctly bind a QObject value in rust?
use qmetaobject::prelude::*
#[derive(QObject, Default)]
struct MyObject1 {
base: qt_base_class!(trait QObject),
my_method: qt_method!(
fn my_method(&self) -> QString {
"Hello QML!".into()
}
),
}
fn main() {
let mut obj_1 = /* ??? what to put here ??? */;
}
:information_source: UPDATE: The following example works. May be useful for an example on the website.
fn main() {
let obj_1 = MyObject1::default();
// FIXME: Does compile; produces runtime error in QML -> Unable to determine callable overload.
// qml_register_singleton_instance(cstr!("Obj1"), 1, 0, cstr!("Obj1"), obj_1);
let mut engine = QmlEngine::new();
let app_interface = engine.new_qobject(obj_1);
engine.set_property("Obj1".into(), obj_1.to_variant()); // TODO: Couldn't get this to work with set_object_property
engine.add_import_path("qml".into());
engine.load_file("qml/Main.qml".into());
engine.exec();
}
However I am not completely happy with this and think a singleton instance is preferable for this (or maybe QJSValue property?). Is the FIXME mentioned actually an issue with QmlEngine's internal initalization sequence? (no other code changes involved). :lady_beetle: :question:
I am pretty new to Rust and simply found the idea intriguing and adapt to an existing QML frontend (this one more precisely). The first steps went pretty smooth starting from the qmetaobject example and I was quickly able to get some visuals rendered on screen. :+1:
However I got a little stuck with how to register a QObject as a root context property to interface with the application backend. A little (functional C++) snippet should illustrate this.
In the C++ application those (global) objects are then accessible in QML.
I could not find an example on how to formulate something like this with qmetaobject-rs. Something like
qml_register_singleton_instance(…)
would also be fine. The question is more like how to correctly bind a QObject value in rust?:information_source: UPDATE: The following example works. May be useful for an example on the website.
However I am not completely happy with this and think a singleton instance is preferable for this (or maybe QJSValue property?). Is the
FIXME
mentioned actually an issue with QmlEngine's internal initalization sequence? (no other code changes involved). :lady_beetle: :question: