pub struct QApp { /* private fields */ }Expand description
Entry point for a Qt QML application.
Wraps the Qt application and QML engine. Configure it with the builder
methods and call run to start the event loop.
§Example
A minimal “Hello World” application without a Rust backend:
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 the Qt application and QML engine.
Must be called before any QML or GUI functionality is used.
Sourcepub fn run(&mut self) -> i32
pub fn run(&mut self) -> i32
Enters the Qt main event loop.
Blocks until the application exits and returns the exit code.
Usually the last call in main.
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
Queues an initial property to be set on the root QML object.
Properties are applied when load_qml or
load_qml_from_file is called.
Call multiple times to set several properties.
§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 multiple initial properties on the root QML object at once.
Must be called before load_qml or
load_qml_from_file.
§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();Sourcepub fn load_qml(&mut self, code: &[u8]) -> &mut QApp
pub fn load_qml(&mut self, code: &[u8]) -> &mut QApp
Loads QML source from an in-memory byte slice.
Applies any properties queued with add_initial_property
before loading.
Sourcepub fn load_qml_from_file(&mut self, url: &str) -> &mut QApp
pub fn load_qml_from_file(&mut self, url: &str) -> &mut QApp
Loads the entry-point QML file by URL.
Use this instead of load_qml when the QML is
embedded in a Qt resource (qrc:) or accessible as a file path.
Accepts URLs such as "qrc:/qt/qml/MyApp/Main.qml" or
"file:///path/to/main.qml".
Import paths for any modules the file uses must be registered with
add_import_path before this call.
Sourcepub fn add_import_path(&mut self, path: &str) -> &mut QApp
pub fn add_import_path(&mut self, path: &str) -> &mut QApp
Adds a directory to the QML engine’s module import search path.
Call before load_qml_from_file when the
loaded QML imports modules from a directory the engine would not
otherwise find. Accepts both URLs and file-system paths.
Sourcepub fn register<T>(&mut self) -> &mut QAppwhere
T: QmlRegister,
pub fn register<T>(&mut self) -> &mut QAppwhere
T: QmlRegister,
Registers T with the QML type system, making it instantiable from QML.
#[derive(Default)]
pub struct Backend {
}
#[qobject]
impl Backend {
}
QApp::new()
.register::<Backend>()
.load_qml(br#"
import QtQuick
import QtQuick.Controls
ApplicationWindow {
Backend {}
}"#)
.run();Sourcepub fn application_name(&mut self, name: &str) -> &mut QApp
pub fn application_name(&mut self, name: &str) -> &mut QApp
Sets the application name reported to the OS.