|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: 2026 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> |
| 3 | +SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com> |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT OR Apache-2.0 |
| 6 | +--> |
| 7 | + |
| 8 | +# Building with Qt Minimal |
| 9 | + |
| 10 | +In this example, we will demostrate how to build the `cxxqt_object.rs` as well as any QML files using the Rust build system. |
| 11 | +Cargo will do the entire build, including downloding and linking to Qt, just like a typical Rust application. |
| 12 | + |
| 13 | +> Note that Qt Minimal does not provide all Qt features or modules and is primarily designed for Qt QML usage. |
| 14 | +
|
| 15 | +Note that the folder structure of this example is different to the CMake tutorial. |
| 16 | +The CMake example uses a `rust` folder where the Rust part of the project resides in. |
| 17 | +In this setup we'll stick with a standard Cargo folder layout with just the added `qml` folder next to the `src` folder. |
| 18 | + |
| 19 | +The complete example code is available in [`examples/cargo_without_cmake`][cargo-without-cmake] |
| 20 | +in the CXX-Qt repository. |
| 21 | + |
| 22 | +> Note the only difference to [Building with Cargo](./4-cargo-executable.md) is the `qt_minimal` feature and `qt-version` dependency |
| 23 | +
|
| 24 | +> Note `qt-build-utils` will first check for a system Qt install matching the Qt version requirements first and then download Qt minimal if required. |
| 25 | +
|
| 26 | +## Cargo setup |
| 27 | + |
| 28 | +Add the dependencies to the `Cargo.toml` file. |
| 29 | +We'll need `cxx`, `cxx-qt`, `cxx-qt-lib`, `cxx-qt-build`, `qt-build-utils`, and `qt-version`: |
| 30 | + |
| 31 | +```toml,ignore |
| 32 | +{{#include ../../../examples/qml_minimal/rust/Cargo.toml:book_package_name}} |
| 33 | +{{#include ../../../examples/cargo_without_cmake/Cargo.toml:book_cargo_toml_no_cmake}} |
| 34 | +cxx = "1.0.95" |
| 35 | +cxx-qt = "0.8" |
| 36 | +cxx-qt-lib = { version="0.8", features = ["qt_full"] } |
| 37 | +
|
| 38 | +[build-dependencies] |
| 39 | +# The link_qt_object_files feature is required for statically linking Qt 6. |
| 40 | +cxx-qt-build = { version = "0.8", features = [ "link_qt_object_files" ] } |
| 41 | +
|
| 42 | +# Enable the qt_minimal feature in qt-build-utils |
| 43 | +# this allows for automatically downloading Qt when not found with qmake |
| 44 | +qt-build-utils = { version = "0.8", features = ["qt_minimal"] } |
| 45 | +# Indicate which Qt version is required via features |
| 46 | +qt-version = { version = "0.1", features = ["qt_version_at_least_6_10"] } |
| 47 | +``` |
| 48 | + |
| 49 | +Now we'll add a `build.rs` script next to the `Cargo.toml` file. |
| 50 | + |
| 51 | +```rust,ignore |
| 52 | +{{#include ../../../examples/cargo_without_cmake/build.rs:book_cargo_executable_build_rs}} |
| 53 | +``` |
| 54 | + |
| 55 | +This is what generates and compiles the C++ code for our `MyObject` class at build time. |
| 56 | +It will also link Qt to our Rust binary. |
| 57 | + |
| 58 | +Every Rust source file that uses the `#[cxx_qt::bridge]` macro needs to be included in this script. |
| 59 | +In our case, this is only the `src/cxxqt_object.rs` file. |
| 60 | + |
| 61 | +This is also where the QML module is defined with a QML URI and version. |
| 62 | +The files and resources in the module are then exposed in the same way as the [`qt_add_qml_module` CMake function](https://doc.qt.io/qt-6/qt-add-qml-module.html). |
| 63 | + |
| 64 | +Refer to the [`CxxQtBuilder`](https://docs.rs/cxx-qt-build/latest/cxx_qt_build/struct.CxxQtBuilder.html) |
| 65 | +and [`cc::Build`](https://docs.rs/cc/latest/cc/struct.Build.html) documentation for further details. |
| 66 | + |
| 67 | +## Rust executable |
| 68 | + |
| 69 | +In `src/main.rs`, first import the `cxxqt_object` module and some types we will need to run our Qt application: |
| 70 | + |
| 71 | +```rust,ignore |
| 72 | +{{#include ../../../examples/cargo_without_cmake/src/main.rs:book_cargo_imports}} |
| 73 | +``` |
| 74 | + |
| 75 | +Define the `main` function that will be called when the executable starts. This works just like starting a QML |
| 76 | +application in C++: |
| 77 | + |
| 78 | +- Create a `QGuiApplication` |
| 79 | +- Create a `QQmlApplicationEngine` |
| 80 | +- Set the QML file path to the engine |
| 81 | +- Run the application |
| 82 | + |
| 83 | +```rust,ignore |
| 84 | +{{#include ../../../examples/cargo_without_cmake/src/main.rs:book_cargo_rust_main}} |
| 85 | +``` |
| 86 | + |
| 87 | +To build and run the application, use `cargo run`. |
| 88 | + |
| 89 | +If this fails for any reason, take a look at the [`examples/cargo-without-cmake`][cargo-without-cmake] folder in the CXX-Qt repository, which contains the complete example code. |
| 90 | + |
| 91 | +If you have cloned the CXX-Qt repository, you can run this example from within the repository using: |
| 92 | + |
| 93 | +```shell |
| 94 | +cargo run -p qml-minimal-no-cmake |
| 95 | +``` |
| 96 | + |
| 97 | +You should now see the two Labels that display the state of our `MyObject`, as well as the two buttons to call our two Rust functions. |
| 98 | + |
| 99 | +## Success 🥳 |
| 100 | + |
| 101 | +For further reading, you can take a look at the [bridge chapter](../bridge/index.md) which goes into detail about all features that CXX-Qt exposes to new `QObject` subclasses. |
| 102 | +As well as the [Concepts chapter](../concepts/index.md), which explains the concepts underlying CXX-Qt. |
| 103 | + |
| 104 | +[cargo-without-cmake]: https://github.com/KDAB/cxx-qt/tree/main/examples/cargo_without_cmake |
0 commit comments