QApp

Struct QApp 

Source
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

Source

pub fn new() -> QApp

Creates a new application instance.

The application object must be created before any QML or GUI-related functionality is used.

Source

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.

Source

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

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.

Source

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.

Source

pub fn register<T>(&mut self) -> &mut QApp
where 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();
Source

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

Sets the application name.

§Parameters
  • name – The application name.
§Returns

A mutable reference to self, allowing method chaining.

Auto Trait Implementations§

§

impl Freeze for QApp

§

impl RefUnwindSafe for QApp

§

impl !Send for QApp

§

impl !Sync for QApp

§

impl Unpin 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.