diff --git a/crates/cxx-qt-build/src/lib.rs b/crates/cxx-qt-build/src/lib.rs index 602818d79..dc2865e39 100644 --- a/crates/cxx-qt-build/src/lib.rs +++ b/crates/cxx-qt-build/src/lib.rs @@ -377,6 +377,7 @@ pub struct CxxQtBuilder { include_prefix: String, crate_include_root: Option, additional_include_dirs: Vec, + qml_qobjects: Vec<(PathBuf, PathBuf)>, } impl CxxQtBuilder { @@ -418,6 +419,7 @@ impl CxxQtBuilder { include_prefix: crate_name(), crate_include_root: Some(String::new()), additional_include_dirs: vec![], + qml_qobjects: vec![], } } @@ -588,6 +590,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( + mut self, + qobject_header: impl AsRef, + qobject_source: impl AsRef, + ) -> 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 @@ -869,6 +887,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().compile( + 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,