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
impl CxxQtBuilder
sourcepub fn file(self, rust_source: impl AsRef<Path>) -> Self
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
sourcepub fn qrc(self, qrc_file: impl AsRef<Path>) -> Self
pub fn qrc(self, qrc_file: impl AsRef<Path>) -> Self
Generate C++ files from Qt resource .qrc files.
The generated file needs to be #include
d 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.
sourcepub fn qt_module(self, module: &str) -> Self
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.
sourcepub fn qml_module<A: AsRef<Path>, B: AsRef<Path>>(
self,
qml_module: QmlModule<'_, A, B>
) -> CxxQtBuilder
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();
sourcepub fn qobject_header(self, path: impl AsRef<Path>) -> Self
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.
sourcepub fn cc_builder(self, callback: impl FnMut(&mut Build)) -> Self
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();
sourcepub fn build(self)
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
impl Default for CxxQtBuilder
source§fn default() -> CxxQtBuilder
fn default() -> CxxQtBuilder
Auto Trait Implementations§
impl Freeze for CxxQtBuilder
impl RefUnwindSafe for CxxQtBuilder
impl Send for CxxQtBuilder
impl Sync for CxxQtBuilder
impl Unpin for CxxQtBuilder
impl UnwindSafe for CxxQtBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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