Skip to main content

qtbridge_interfaces/qparser_status/
proxy_rust.rs

1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use qtbridge_runtime::{DispatchMetaCall, QObjectHolder};
5use crate::call_rust_trait_impl;
6use crate::genericrustproxy::GenericRustProxy;
7use super::proxy_cpp_bridge::QParserStatusProxyCpp;
8
9#[allow(clippy::needless_doctest_main)]
10/// A trait for hooking into QML component construction stages.
11///
12/// [`QParserStatus::class_begin`] is called first when the
13/// object is created, followed by [`QParserStatus::component_complete`] once
14/// all QML properties have been set. This allows you to defer expensive
15/// initialization until the component is fully constructed.
16///
17/// This trait is the Rust equivalent of
18/// [`QQmlParserStatus`](https://doc.qt.io/qt-6/qqmlparserstatus.html).
19///
20/// This trait requires the `qobject` macro with `Base = QParserStatus` to set
21/// up the correct Qt proxy.
22///
23/// ## Example
24///
25/// ```rust
26/// use qtbridge::{QApp, qobject};
27///
28/// #[qobject(Base = QParserStatus)]
29/// pub mod backend {
30///     use qtbridge::QParserStatus;
31///
32///     #[derive(Default)]
33///     pub struct Status {
34///         somewhat_ready: bool,
35///     }
36///
37///     impl Status {
38///         #[qsignal]
39///         fn ready(&mut self);
40///     }
41///
42///     impl QParserStatus for Status {
43///         fn class_begin(&mut self) {
44///             self.somewhat_ready = true;
45///         }
46///
47///         fn component_complete(&mut self) {
48///             if self.somewhat_ready {
49///                 self.ready();
50///             }
51///         }
52///     }
53/// }
54///
55/// const QML_CODE: &str =
56/// r#"
57///     import QtQuick
58///     import QtQuick.Controls
59///#     import qtbridge_interfaces
60///     // import your module
61///
62///     ApplicationWindow {
63///         visible: true
64///         Status {
65///             onReady: closeTimer.start()
66///         }
67///         Timer {
68///             id: closeTimer
69///             interval: 1
70///             onTriggered: Qt.quit()
71///         }
72///     }
73/// "#;
74///
75/// fn main() {
76///     QApp::new()
77///         .register::<backend::Status>()
78///         .load_qml(QML_CODE.as_bytes())
79///         .run();
80/// }
81/// ```
82pub trait QParserStatus {
83    /// Called when the object is first created, before any properties are set.
84    ///
85    /// Use this to perform any initialization that must happen before
86    /// properties are applied.
87    fn class_begin(&mut self) {}
88
89    /// Called after all QML properties have been set.
90    ///
91    /// Use this to perform initialization that depends on property values,
92    /// or to emit signals that notify QML the object is ready.
93    fn component_complete(&mut self) {}
94}
95
96#[doc(hidden)]
97pub trait QParserStatusAdapter: QParserStatus + DispatchMetaCall { }
98
99impl<T> QParserStatusAdapter for T
100where T: QParserStatus + QObjectHolder<ProxyRust = QParserStatusProxyRust> { }
101
102pub type QParserStatusProxyRust = GenericRustProxy<QParserStatusProxyCpp, dyn QParserStatusAdapter>;
103
104impl<T: QParserStatusAdapter + 'static> qtbridge_runtime::qproxies::AdapterUpcast<T> for QParserStatusProxyRust {
105    fn upcast(rc: std::rc::Rc<std::cell::RefCell<T>>) -> std::rc::Rc<std::cell::RefCell<dyn QParserStatusAdapter>> {
106        rc
107    }
108}
109
110impl QParserStatusProxyRust {
111    pub fn class_begin(&mut self) {
112        call_rust_trait_impl!(mut self, class_begin())
113    }
114
115    pub fn component_complete(&mut self) {
116        call_rust_trait_impl!(mut self, component_complete())
117    }
118}