You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. 👍
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)]structMyObject1{base:qt_base_class!(traitQObject),my_method:qt_method!(fnmy_method(&self) -> QString{"Hello QML!".into()}),}fnmain(){letmut obj_1 = /* ??? what to put here ??? */;}
ℹ️ UPDATE: The following example works. May be useful for an example on the website.
fnmain(){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);letmut 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). 🐞 ❓
The text was updated successfully, but these errors were encountered:
antis81
changed the title
How to instantiate a QObject and make it globally available to QmlEngine's root context?
Example how to create a global application interface object available as QmlEngine root context property
Nov 14, 2023
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. 👍
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?ℹ️ 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). 🐞 ❓The text was updated successfully, but these errors were encountered: