Skip to content

Commit 82efc03

Browse files
author
Alexander Kiselev
committed
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
1 parent f5afefe commit 82efc03

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/cxx-qt-build/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ pub struct CxxQtBuilder {
358358
public_interface: Option<Interface>,
359359
include_prefix: String,
360360
initializers: Vec<String>,
361+
qml_qobjects: Vec<(PathBuf, PathBuf)>,
361362
}
362363

363364
impl CxxQtBuilder {
@@ -398,6 +399,7 @@ impl CxxQtBuilder {
398399
initializers: vec![],
399400
public_interface: None,
400401
include_prefix: crate_name(),
402+
qml_qobjects: vec![],
401403
}
402404
}
403405

@@ -532,6 +534,22 @@ impl CxxQtBuilder {
532534
self
533535
}
534536

537+
/// Specify a C++ header and source file containing a Q_OBJECT macro to run [moc](https://doc.qt.io/qt-6/moc.html) on.
538+
/// Unlike [CxxQtBuilder::qobject_header], it includes the generated metatypes.json, so that C++ classes can be included
539+
/// in QML modules.
540+
pub fn qml_qobject(
541+
mut self,
542+
qobject_header: impl AsRef<Path>,
543+
qobject_source: impl AsRef<Path>,
544+
) -> Self {
545+
let qobject_header = qobject_header.as_ref().to_path_buf();
546+
let qobject_source = qobject_source.as_ref().to_path_buf();
547+
println!("cargo::rerun-if-changed={}", qobject_header.display());
548+
println!("cargo::rerun-if-changed={}", qobject_source.display());
549+
self.qml_qobjects.push((qobject_header, qobject_source));
550+
self
551+
}
552+
535553
/// Use a closure to run additional customization on [CxxQtBuilder]'s internal [cc::Build]
536554
/// before calling [CxxQtBuilder::build]. This allows to add extra include paths, compiler flags,
537555
/// or anything else available via [cc::Build]'s API. For example, to add an include path for
@@ -844,6 +862,22 @@ impl CxxQtBuilder {
844862
}
845863
}
846864

865+
for (qobject_header, qobject_source) in &self.qml_qobjects {
866+
cc_builder.file(qobject_source);
867+
if let Some(dir) = qobject_header.parent() {
868+
moc_include_paths.insert(dir.to_path_buf());
869+
}
870+
let moc_products = qtbuild.moc(
871+
qobject_header,
872+
MocArguments::default().uri(qml_module.uri.clone()),
873+
);
874+
if let Some(dir) = moc_products.cpp.parent() {
875+
moc_include_paths.insert(dir.to_path_buf());
876+
}
877+
cc_builder.file(moc_products.cpp);
878+
qml_metatypes_json.push(moc_products.metatypes_json);
879+
}
880+
847881
let qml_module_registration_files = qtbuild.register_qml_module(
848882
&qml_metatypes_json,
849883
&qml_module.uri,

0 commit comments

Comments
 (0)