Skip to main content

qtbridge_interfaces/
lib.rs

1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4//! # Qt Interfaces (Internal Documentation)
5//!
6//! This crate provides proxies for various Qt (C++) interfaces using Cxx.
7//! Proxies serve as the bridge between Rust and C++ implementations of Qt
8//! interfaces, allowing Rust types to implement behavior expected by C++
9//! code and to access C++ functionality from Rust.
10//!
11//! ## The proxy
12//!
13//! Each Qt interface has a corresponding **proxy** in Rust. This proxy has
14//! to implement the [`qtbridge_runtime::qproxies::QRustProxy`] trait and interacts mostly with
15//! the [`qtbridge_runtime::QObjectHolder`] trait, that is automatically implemented for all
16//! user types by the `qobject` macro.
17//!
18//! The demands on proxies are baked into the [`qtbridge_runtime::qproxies::QRustProxy`] trait.
19//! For example, there
20//! is the demand to provide a Qt static meta object, usually from the "base" this proxy
21//! is derived from. Further, the proxy has to provide a marker trait called `AdapterType`
22//! that is at a minimum used to store a reference to the user object as
23//! `Rc<RefCell<dyn AdapterType>>`. Otherwise the proxy is mostly free in its design.
24//!
25//! All proxies provided in this module follow a similar architecture. The proxy works
26//! together with a set of traits. The QObject proxy is very different since it is the
27//! proxy that is used if only QMetaObject functionality needs to be provided but no
28//! interface implementation.
29//!
30//! ## Traits
31//!
32//! 1. **User-implemented functionality**: Rust implementations of Qt virtual functions.
33//! 2. **Rust access to C++ functionality**: Rust wrappers around C++ methods of the interface.
34//! 3. **Internal type erasure and proxy plumbing**: Allows dynamic behavior when types
35//!    are not fully known at compile time.
36//!
37//! These proxies rely on the following Rust traits to define functionality and structure,
38//! shown on the example of `QListModel`.
39//!
40//! ### 1. `QListModel`
41//! * **Responsibility**: Must be implemented by the user.
42//! * **Purpose**: Functions in this trait are called from C++ via the proxy, providing Rust
43//!   implementations of Qt virtual functions. Some modifications can be done in the functions
44//!   to shape the API and to erase Qt types (e.g. QVariant and QModelIndex).
45//! * **Associated Types**: Defines types (e.g., `Item`) that are part of the interface contract.
46//! * **Notes**: Users implement function as expected from the Qt API. Default implementation
47//!   can exist for non-pure virtual functions.
48//!
49//! ### 2. `QListModelBase`
50//! * **Responsibility**: Default / blank implementation for all `QListModel`.
51//! * **Purpose**: Provides access to C++ functions from Rust and other convenience functions
52//!   that should not be overridden. Serves as a bridge between user logic and C++ functionality.
53//!   Functions that should not be overridden by the user are implemented here.
54//! * **Notes**: This trait forms the core Rust interface to call C++ functions for types implementing
55//!   `QListModel`.
56//!
57//! ### 3. `QListModelAdapter` (Internal Only)
58//! * **Responsibility**: Default / blank implementation for all `QListModel`.
59//! * **Purpose**: Provides a type-erased interface for internal machinery. Traits with associated
60//!   types (like `QListModel::Item`) cannot be used as `dyn` traits in Rust. `QListModelAdapter`
61//!   erases unknown types while exposing the necessary interface internally.
62//! * **Notes**: Never meant for user implementation or usage. Facilitates dynamic behavior and
63//!   internal bridging without exposing associated types to user code.
64//!
65//! ## Memory Ownership and Lifetimes
66//!
67//! Every instance of a user-defined Rust struct annotated with `#[qobject]`
68//! has two auxiliary parts invisible to the user:
69//!
70//! - **`CppProxy`** - a C++ class derived from`QObject` or `QAbstractItemModel` (or another base
71//!   interface). This is the object the QML engine sees and interacts with. It is allocated with
72//!   a regular `new` for Rust-created objects, or with placement `new` at a QML-engine-supplied
73//!   address for QML-created elements.
74//! - **[`RustProxy`](crate::genericrustproxy::GenericRustProxy)** - the Rust-side bridge,
75//!   heap-allocated via [`Box::into_raw`] in
76//!   [`QRustProxy::new`](qtbridge_runtime::qproxies::QRustProxy::new). It holds a pointer to
77//!   `CppProxy` and a reference the user struct as `Rc<RefCell<UserStruct>>` or
78//!   `Weak<RefCell<UserStruct>>` in [`RustObjAccess`].
79//!
80//! The `CppProxy` C++ destructor is the common teardown point for the proxy pair: it always
81//! calls `GenericRustProxy::drop_self`, which fires the `on_drop` callback and then drops
82//! `RustProxy` along with its `Rc`/`Weak` reference to the user struct.
83//!
84//! ### Who destroys `CppProxy`?
85//!
86//! That depends on who owns the object, determined at construction by `ConstructionMode`.
87//!
88//! #### QML-created objects
89//!
90//! `RustProxy` holds a strong `Rc<RefCell<UserStruct>>` (`SharedReferenceWithQml::OwnedByQml`).
91//!
92//! ```text
93//! JS GC deletes QObject
94//!   → virtual ~CppProxy()
95//!   → GenericRustProxy::drop_self
96//!     → on_drop() fires
97//!     → Rc<RefCell<UserStruct>> strong count −1
98//!       → UserStruct::drop()                     (only if this was the last strong Rc)
99//! ```
100//!
101//! #### Rust-created objects
102//!
103//! `RustProxy` holds only a `Weak<RefCell<UserStruct>>`
104//! (`SharedReferenceWithQml::OwnedByRust`). The Rust `Rc` controls the struct's lifetime.
105//! When the last strong `Rc` is dropped, `QObjectHolder::detach_qobject` deletes `CppProxy`.
106//!
107//! ```text
108//! Last strong Rc<RefCell<UserStruct>> dropped on the Rust side
109//!   → UserStruct::drop()
110//!     → QObjectHolder::detach_qobject()
111//!       → virtual ~CppProxy()
112//!       → GenericRustProxy::drop_self
113//!         → on_drop()
114//! ```
115
116pub mod genericrustproxy;
117
118pub mod qobject;
119
120pub mod qabstract_item_model;
121pub use qabstract_item_model::{QAbstractItemModel, QAbstractItemModelBase};
122
123pub mod qlist_model;
124pub use qlist_model::{QListModel, QListModelBase};
125
126pub mod qparser_status;
127pub use qparser_status::QParserStatus;
128
129pub mod qtable_model;
130pub use qtable_model::{QTableModel, QTableModelBase};
131
132pub mod object_access;
133pub use object_access::rust_object_access::RustObjAccess;