pub struct QApp { /* private fields */ }Expand description
This struct represents a Qt QML application and acts as the entry point for all applications.
QApp allows running QML code and injecting Rust objects into its context. Aside from the initialization of the backend logic this should be the only code in your main function.
§Example
A minimal “Hello World” application without a Rust backend:
let prop = 42;
QApp::new()
.load_qml(br#"
import QtQuick
import QtQuick.Controls
Text {
text: "Hello Rust!"
}"#)
.run();Implementations§
Source§impl QApp
impl QApp
Sourcepub fn new() -> QApp
pub fn new() -> QApp
Creates a new application instance.
The application object must be created before any QML or GUI-related functionality is used.
Sourcepub fn run(&mut self) -> i32
pub fn run(&mut self) -> i32
Enters the Qt main event loop.
This function blocks until the application exits and returns
the exit code provided by Qt. This function does not return a reference to
self and is usually the last call in a main function.
§Returns
The application exit code.
Sourcepub fn add_initial_property(&mut self, id: &str, value: &QVariant) -> &mut QApp
pub fn add_initial_property(&mut self, id: &str, value: &QVariant) -> &mut QApp
Add an initial property to the root object of the QML application.
This method takes a string slice for the property name and a reference to a QVariant
as the property value. The property is stored internally and applied when QApp::load_qml
is called.
§Arguments
id- The name of the property to add to the root QML object.value- The value of the property.
§Example
let prop = 42;
QApp::new()
.add_initial_property("answer", &prop.into())
.load_qml(br#"
import QtQuick
import QtQuick.Controls
ApplicationWindow {
required property var answer
}"#)
.run();Sourcepub fn with_initial_properties(
&mut self,
properties: &[(&str, QVariant)],
) -> &mut QApp
pub fn with_initial_properties( &mut self, properties: &[(&str, QVariant)], ) -> &mut QApp
Sets the initial properties of the root object in the QML application.
This method passes a list of str-QVariant pairs to the engine
and you can read and write the value in your QML code.
§Returns
A mutable reference to self, allowing method chaining.
§Example
let prop = 42;
QApp::new()
.with_initial_properties(&[
("answer", prop.into()),
])
.load_qml(br#"
import QtQuick
import QtQuick.Controls
ApplicationWindow {
required property var answer
}"#)
.run();§Returns
A mutable reference to self, allowing method chaining.
Sourcepub fn load_qml(&mut self, code: &[u8]) -> &mut QApp
pub fn load_qml(&mut self, code: &[u8]) -> &mut QApp
Loads the main QML source code from an in-memory byte slice.
This method loads the given QML source into the application’s QML engine. It is typically used to initialize the UI before entering the Qt event loop.
§Parameters
code– A byte slice containing the QML source.
§Returns
A mutable reference to self, allowing method chaining.
Sourcepub fn register<T>(&mut self) -> &mut QAppwhere
T: QmlRegister,
pub fn register<T>(&mut self) -> &mut QAppwhere
T: QmlRegister,
Registers a QML type, making it instantiable from QML.
This is a convenience wrapper that calls QmlRegister::register
for the given type T.
#[derive(Default)]
pub struct Backend {
}
#[qobject_impl]
impl Backend {
}
QApp::new()
.register::<Backend>()
.load_qml(br#"
import QtQuick
import QtQuick.Controls
ApplicationWindow {
Backend {}
}"#)
.run();