Skip to main content

QApp

Struct QApp 

Source
pub struct QApp { /* private fields */ }
Expand description

Entry point for a 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

Source

pub fn new() -> QApp

Creates the Qt application and QML engine.

Must be called before any QML or GUI functionality is used.

Source

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.

Source

pub fn set_initial_property( &mut self, id: &str, value: impl Into<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()
.set_initial_property("answer", &prop)
.load_qml(br#"
    import QtQuick
    import QtQuick.Controls
    ApplicationWindow {
        required property var answer
    }"#)
.run();
Source

pub fn set_initial_object<T>( &mut self, id: &str, object: Rc<RefCell<T>>, ) -> &mut QApp
where T: QObjectHolder,

Sets a #[qobject] instance as initial property on the root QML object, attaching a QObject to it first if none exists.

Must be called before load_qml or load_qml_from_file. Call multiple times to set several objects.

§Example
#[derive(Default)]
pub struct Backend {
}
#[qobject]
impl Backend {
}

let backend = Rc::new(RefCell::new(Backend::default()));

QApp::new()
    .set_initial_object("backend", backend)
    .load_qml(br#"
        import QtQuick
        Item {
            required property var backend
        }"#)
    .run();
Source

pub fn load_qml(&mut self, code: &[u8]) -> &mut QApp

Loads QML source from an in-memory byte slice.

Applies any properties queued with set_initial_property before loading.

Source

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.

Source

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.

Source

pub fn register<T>(&mut self) -> &mut QApp
where T: QmlElement,

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();
Source

pub fn application_name(&mut self, name: &str) -> &mut QApp

Sets the application name reported to the OS.

Trait Implementations§

Source§

impl Drop for QApp

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for QApp

§

impl !RefUnwindSafe for QApp

§

impl !Send for QApp

§

impl !Sync for QApp

§

impl Unpin for QApp

§

impl UnsafeUnpin for QApp

§

impl !UnwindSafe for QApp

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.