Skip to content

Commit

Permalink
book: improve descriptions around folder structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab authored and Be-ing committed Nov 18, 2022
1 parent 224baad commit f36a2a5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
17 changes: 13 additions & 4 deletions book/src/getting-started/2-our-first-cxx-qt-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@ SPDX-License-Identifier: MIT OR Apache-2.0

# Our first CXX-Qt module

As with all things Rust, we'll first want to create a cargo project.
We first need to create a folder structure to add the different parts of our project.

```ignore
tutorial
- cpp
- qml
- rust
```

As with all things Rust, we'll want to create a cargo project, run the following command inside the `tutorial` folder to initialise the Rust part of the project.
```bash
cargo new --lib qml-minimal
cargo init --lib rust
```
Note the `--lib` option here. For this example, we will create a static library in Rust and use CMake to
link this into a C++ executable. We'll discuss details of this later, when we [integrate our Rust project with CMake](./5-cmake-integration.md).

As outlined in the previous section, to define a new QObject subclass, we'll create a Rust module within this library crate.
First, in the `src/lib.rs`, we tell Cargo about the module we're about to create:
First, in the `rust/src/lib.rs`, we tell Cargo about the module we're about to create:

```rust,ignore
{{#include ../../../examples/qml_minimal/rust/src/lib.rs:book_mod_statement}}
```

Now, we need to create a file `src/cxxqt_object.rs` for that module.
Now, we need to create a file `rust/src/cxxqt_object.rs` for that module.
It will include our `#[cxx_qt::bridge]` that allows us to create our own qobjects in Rust:

```rust,ignore
Expand Down
11 changes: 7 additions & 4 deletions book/src/getting-started/5-cmake-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ as a static library, then CMake links it into a C++ executable.
## Cargo setup
Before we can get started on building Qt with CMake, we first need to make our Cargo build ready for it.
If you've generated your project with the `cargo new --lib` command, your `Cargo.toml` likely looks something like this:
If you've generated your project with the `cargo new --lib` or `cargo init --lib folder` command, your `Cargo.toml` likely looks something like this:
```toml,ignore
[package]
name = "qml-minimal"
Expand All @@ -31,12 +31,15 @@ We'll have to do multiple things:
- Add `cxx`, `cxx-qt`, as well as `cxx-qt-lib` as dependencies
- Add `cxx-qt-build` as a build dependency

In the end, your `Cargo.toml` should look similar to this (note that `path` for the dependencies is only
needed for the example within the cxx-qt repository; in your project you'd use the version from crates.io):
In the end, your `Cargo.toml` should look similar to this.

```toml,ignore
{{#include ../../../examples/qml_minimal/rust/Cargo.toml:book_all}}
```

> Note that instead of the `{ path = "..." }` arguments for the CXX-Qt crates, you should instead use the versions from [crates.io](https://crates.io/search?q=cxx-qt).
> As described in the code comment above each dependency.
We'll then also need to add a script named `build.rs` next to our `Cargo.toml`:
```rust,ignore
{{#include ../../../examples/qml_minimal/rust/build.rs:book_build_rs}}
Expand All @@ -49,7 +52,7 @@ In our case, this is only the `src/cxxqt_object.rs` file.

## CMake setup

Start the CMakeLists.txt file like any other C++ project using Qt. For this example, we are [supporting both
Now add a `CMakeLists.txt` file in the root of the `tutorial` folder. Start the `CMakeLists.txt` file like any other C++ project using Qt. For this example, we are [supporting both
Qt5 and Qt6 with CMake](https://doc.qt.io/qt-6/cmake-qt5-and-qt6-compatibility.html):

```cmake,ignore
Expand Down
13 changes: 8 additions & 5 deletions book/src/getting-started/6-cargo-executable.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ previous example, but without using CMake or another C++ build system. Cargo wil
just like a typical Rust application. Because cxx-qt does not bind the entire Qt API, we will still
need to write a bit of C++ code. However, we'll use the cxx-qt-build crate to compile it instead of CMake.

Note that the folder structure of this example is different to the CMake tutorial, this is because the `Cargo.toml` is now in the root. So there isn't a `rust` folder, instead just a `src` folder and the `.rs` files have moved up one folder.

The complete example code is available in [`examples/cargo_without_cmake`](https://github.com/KDAB/cxx-qt/tree/main/examples/cargo_without_cmake)
in the cxx-qt repository.

Expand All @@ -23,7 +25,8 @@ The Cargo.toml file still requires dependencies to `cxx`, `cxx-qt`, `cxx-qt-lib`
{{#include ../../../examples/cargo_without_cmake/Cargo.toml:book_cargo_toml_no_cmake}}
```

Note that instead of the `{ path = "..." }` arguments for the CXX-Qt crates, you should instead use the versions from [crates.io](https://crates.io/search?q=cxx-qt).
> Note that instead of the `{ path = "..." }` arguments for the CXX-Qt crates, you should instead use the versions from [crates.io](https://crates.io/search?q=cxx-qt).
> As described in the code comment above each dependency.
The `build.rs` script is similar. However, without CMake, CxxQtBuilder needs to do a bit more work:

Expand All @@ -43,26 +46,26 @@ First, we need to include Qt headers, the C++ header generated from `src/cxxqt_o
from the `qml.qrc` file:

```c++,ignore
{{#include ../../../examples/cargo_without_cmake/src/cpp/run.cpp:book_cargo_cpp_includes}}
{{#include ../../../examples/cargo_without_cmake/cpp/run.cpp:book_cargo_cpp_includes}}
```

Instead of the `main` function in a typical C++ application, write an `extern "C"` function which we will call
from Rust:

```c++,ignore
{{#include ../../../examples/cargo_without_cmake/src/cpp/run.cpp:book_cargo_run_cpp}}
{{#include ../../../examples/cargo_without_cmake/cpp/run.cpp:book_cargo_run_cpp}}
```

In this function, we need to initialize the Qt resource system:

```c++,ignore
{{#include ../../../examples/cargo_without_cmake/src/cpp/run.cpp:book_cargo_init_qrc}}
{{#include ../../../examples/cargo_without_cmake/cpp/run.cpp:book_cargo_init_qrc}}
```

Then, register the QML type and run the QML file just like a C++ program:

```c++,ignore
{{#include ../../../examples/cargo_without_cmake/src/cpp/run.cpp:book_cargo_run_qml}}
{{#include ../../../examples/cargo_without_cmake/cpp/run.cpp:book_cargo_run_qml}}
```

## Rust executable
Expand Down
4 changes: 2 additions & 2 deletions examples/cargo_without_cmake/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fn main() {
// Tell CxxQtBuilder's internal cc::Build struct to compile the manually
// written C++ file in addition to the generated C++.
.cc_builder(|cc| {
cc.file("src/cpp/run.cpp");
println!("cargo:rerun-if-changed=src/cpp/run.cpp");
cc.file("cpp/run.cpp");
println!("cargo:rerun-if-changed=cpp/run.cpp");
})
.build();

Expand Down
File renamed without changes.

0 comments on commit f36a2a5

Please sign in to comment.