Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for C++ QML Elements in CxxQtBuilder #1146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ pub struct CxxQtBuilder {
public_interface: Option<Interface>,
include_prefix: String,
initializers: Vec<String>,
qml_qobjects: Vec<(PathBuf, PathBuf)>,
}

impl CxxQtBuilder {
Expand Down Expand Up @@ -398,6 +399,7 @@ impl CxxQtBuilder {
initializers: vec![],
public_interface: None,
include_prefix: crate_name(),
qml_qobjects: vec![],
}
}

Expand Down Expand Up @@ -532,6 +534,22 @@ impl CxxQtBuilder {
self
}

/// Specify a C++ header and source file containing a Q_OBJECT macro to run [moc](https://doc.qt.io/qt-6/moc.html) on.
/// Unlike [CxxQtBuilder::qobject_header], it includes the generated metatypes.json, so that C++ classes can be included
/// in QML modules.
pub fn qml_qobject(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpp_qobject ? I guess this could have other applications outside of QML

mut self,
qobject_header: impl AsRef<Path>,
qobject_source: impl AsRef<Path>,
) -> Self {
let qobject_header = qobject_header.as_ref().to_path_buf();
let qobject_source = qobject_source.as_ref().to_path_buf();
println!("cargo::rerun-if-changed={}", qobject_header.display());
println!("cargo::rerun-if-changed={}", qobject_source.display());
self.qml_qobjects.push((qobject_header, qobject_source));
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
Expand Down Expand Up @@ -844,6 +862,22 @@ impl CxxQtBuilder {
}
}

for (qobject_header, qobject_source) in &self.qml_qobjects {
cc_builder.file(qobject_source);
if let Some(dir) = qobject_header.parent() {
moc_include_paths.insert(dir.to_path_buf());
}
let moc_products = qtbuild.moc(
qobject_header,
MocArguments::default().uri(qml_module.uri.clone()),
);
if let Some(dir) = moc_products.cpp.parent() {
moc_include_paths.insert(dir.to_path_buf());
}
cc_builder.file(moc_products.cpp);
qml_metatypes_json.push(moc_products.metatypes_json);
}

let qml_module_registration_files = qtbuild.register_qml_module(
&qml_metatypes_json,
&qml_module.uri,
Expand Down
Loading