From 4d12f16b2bb3c9b82424d2250ae802a67527690c Mon Sep 17 00:00:00 2001 From: Alexander Kiselev Date: Wed, 18 Dec 2024 16:54:37 -0800 Subject: [PATCH 1/2] Add support for C++ QML QObjects in CxxQtBuilder - Introduce qml_qobjects field to CxxQtBuilder struct - Implement qml_qobject method for adding QML QObject files - Process QML QObjects in build method, including moc generation - Include generated metatypes.json for QML module support - Update rerun-if-changed cargo instructions for added files --- crates/cxx-qt-build/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/cxx-qt-build/src/lib.rs b/crates/cxx-qt-build/src/lib.rs index 602818d79..e6ad7fa5d 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( + 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, From 070fc5fb1bfec607edb848acb70bc78afabf9d69 Mon Sep 17 00:00:00 2001 From: Alexander Kiselev Date: Thu, 7 Aug 2025 17:58:16 -0700 Subject: [PATCH 2/2] fix moc call --- crates/cxx-qt-build/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cxx-qt-build/src/lib.rs b/crates/cxx-qt-build/src/lib.rs index e6ad7fa5d..dc2865e39 100644 --- a/crates/cxx-qt-build/src/lib.rs +++ b/crates/cxx-qt-build/src/lib.rs @@ -892,7 +892,7 @@ impl CxxQtBuilder { if let Some(dir) = qobject_header.parent() { moc_include_paths.insert(dir.to_path_buf()); } - let moc_products = qtbuild.moc( + let moc_products = qtbuild.moc().compile( qobject_header, MocArguments::default().uri(qml_module.uri.clone()), );