Struct cxx_qt_build::CxxQtBuilder

source ·
pub struct CxxQtBuilder { /* private fields */ }
Expand description

Run cxx-qt’s C++ code generator on Rust modules marked with the cxx_qt::bridge macro, compile the code, and link to Qt. This is the complement of the cxx_qt::bridge macro, which the Rust compiler uses to generate the corresponding Rust code. No dependencies besides Qt, a C++17 compiler, and Rust toolchain are required.

For example, if your cxx_qt::bridge module is in a file called src/lib.rs within your crate, put this in your build.rs:

use cxx_qt_build::CxxQtBuilder;

CxxQtBuilder::new()
    .file("src/lib.rs")
    .build();

If you have multiple major versions of Qt installed (for example, 5 and 6), you can tell CxxQtBuilder which one to use by setting the QT_VERSION_MAJOR environment variable to when running cargo build. Otherwise CxxQtBuilder prefers the newer version by default.

To use CxxQtBuilder for a library to link with a C++ application, specify a directory to output cxx-qt’s autogenerated headers by having the C++ build system set the CXXQT_EXPORT_DIR environment variable before calling cargo build. Then, add the same directory path to the C++ include paths. Also, set the QMAKE environment variable to the path of the qmake executable for the Qt installation found by the C++ build system. This ensures that the C++ build system and CxxQtBuilder link to the same installation of Qt.

Under the hood, CxxQtBuilder uses cc::Build, which allows compiling aditional C++ files as well. Refer to CxxQtBuilder::cc_builder for details.

In addition to autogenerating and building QObject C++ subclasses, manually written QObject subclasses can be parsed by moc and built using CxxQtBuilder::qobject_header.

Implementations§

source§

impl CxxQtBuilder

source

pub fn new() -> Self

Create a new builder

source

pub fn file(self, rust_source: impl AsRef<Path>) -> Self

Specify rust file paths to parse through the cxx-qt marco Relative paths are treated as relative to the path of your crate’s Cargo.toml file

source

pub fn qrc(self, qrc_file: impl AsRef<Path>) -> Self

Generate C++ files from Qt resource .qrc files. The generated file needs to be #included in another .cpp file. For example:

CxxQtBuilder::new()
    .file("src/cxxqt_module.rs")
    .qrc("src/my_resources.qrc")
    .cc_builder(|cc| {
        cc.file("file_with_include.cpp");
    })
    .build();

In file_with_include.cpp:

#include "my_resources.qrc.cpp"

You also need to explicitly load the resources in your .cpp file by calling qInitResources() once before starting your application.

source

pub fn qt_module(self, module: &str) -> Self

Link additional Qt modules. Specify their names without the Qt prefix, for example "Widgets". The Core and any feature enabled modules are linked automatically; there is no need to specify them.

source

pub fn qml_module<A: AsRef<Path>, B: AsRef<Path>>( self, qml_module: QmlModule<'_, A, B> ) -> CxxQtBuilder

Register a QML module at build time. The rust_files of the QmlModule struct should contain #[cxx_qt::bridge] modules with QObject types annotated with #[qml_element].

The QmlModule struct’s qml_files are registered with the Qt Resource System in the default QML import path qrc:/qt/qml/uri/of/module/. Additional resources such as images can be added to the Qt resources for the QML module by specifying the qrc_files field.

When using Qt 6, this will run qmlcachegen to compile the specified .qml files ahead-of-time.

use cxx_qt_build::{CxxQtBuilder, QmlModule};

CxxQtBuilder::new()
    .qml_module(QmlModule {
        uri: "com.kdab.cxx_qt.demo",
        rust_files: &["src/cxxqt_object.rs"],
        qml_files: &["qml/main.qml"],
        ..Default::default()
    })
    .build();
source

pub fn qobject_header(self, path: impl AsRef<Path>) -> Self

Specify a C++ header containing a Q_OBJECT macro to run moc on. This allows building QObject C++ subclasses besides the ones autogenerated by cxx-qt.

source

pub fn cc_builder(self, callback: impl FnMut(&mut Build)) -> Self

Use a closure to run additional customization on CxxQtBuilder’s internal cc::Build before calling CxxQtBuilder::build. This allows to add extra include paths, compiler flags, or anything else available via cc::Build’s API. For example, to add an include path for manually written C++ headers located in a directory called include within your crate:


CxxQtBuilder::new()
    .file("src/lib.rs")
    .cc_builder(|cc| {
        cc.include("include");
    })
    .build();
source

pub fn build(self)

Generate and compile cxx-qt C++ code, as well as compile any additional files from CxxQtBuilder::qobject_header and CxxQtBuilder::cc_builder.

Trait Implementations§

source§

impl Default for CxxQtBuilder

source§

fn default() -> CxxQtBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.