qtbridge_runtime/
qresource.rs

1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4
5//! This module contains functions to import of artifacts the
6//! [Qt Resource System](https://doc.qt.io/qt-6/resources.html).
7//!
8//! All artifacts must be compiled into dynamic resources with the
9//! [`rcc` tool](https://doc.qt.io/qt-6/rcc.html):
10//! ```bash, ignore
11//! rcc -binary [more options] <inputs>
12//! ```
13//! before or at compile time.
14//! The input for the `rcc` tool are Qt Resource Collection (*.qrc) files,
15//! that contain lists of all files to be imported.
16//! ```xml, ignore
17//! <RCC>
18//!     <qresource prefix="/">
19//!         <file>images/copy.png</file>
20//!         <file>images/cut.png</file>
21//!         ...
22//!     </qresource>
23//! </RCC>
24//! ```
25//!
26//! Dynamic resources can be registered with [`register_bytes`] and
27//! are then available at runtime through the `qrc:/` scheme in QML:
28//! ```qml, ignore
29//! Image {
30//!     source: "qrc:/images/copy.png"
31//! }
32//! ```
33//!
34//! We further provide the [`include_bytes_qml`] macro that generates the
35//! required dynamic resource directly in Rust at compile time.
36#[cxx::bridge]
37mod ffi {
38    unsafe extern "C++" {
39        include!("cpp/qresource.h");
40
41        fn register_resource(data: &[u8], resource_root: &str) -> bool;
42    }
43}
44
45/// Registers Qt resource data from an in-memory byte slice.
46///
47/// This function registers a dynamic resource generated with `rcc`,
48/// making the embedded resources available at runtime through the `qrc:/` scheme.
49///
50/// # Parameters
51///
52/// * `data` - A byte slice containing the compiled Qt resource data.
53///
54/// # Returns
55///
56/// Returns `true` if the resource was successfully registered, or `false`
57/// if registration failed.
58pub fn register_bytes(data: &[u8]) -> bool {
59    ffi::register_resource(data, "")
60}
61
62/// Registers Qt resource data from an in-memory byte slice with a prefix.
63///
64/// This function registers a dynamic resource generated with `rcc`,
65/// making the embedded resources available at runtime through the `qrc:/` scheme.
66///
67/// # Parameters
68///
69/// * `data` - A byte slice containing the compiled Qt resource data.
70/// * `resource_root` - A string slice with the prefix under which the resources
71///    are registered
72///
73/// # Returns
74///
75/// Returns `true` if the resource was successfully registered, or `false`
76/// if registration failed.
77pub fn register_bytes_with_prefix(data: &[u8], resource_root: &str) -> bool {
78    ffi::register_resource(data, resource_root)
79}