@@ -377,6 +377,7 @@ pub struct CxxQtBuilder {
377377 include_prefix : String ,
378378 crate_include_root : Option < String > ,
379379 additional_include_dirs : Vec < PathBuf > ,
380+ qml_qobjects : Vec < ( PathBuf , PathBuf ) > ,
380381}
381382
382383impl CxxQtBuilder {
@@ -418,6 +419,7 @@ impl CxxQtBuilder {
418419 include_prefix : crate_name ( ) ,
419420 crate_include_root : Some ( String :: new ( ) ) ,
420421 additional_include_dirs : vec ! [ ] ,
422+ qml_qobjects : vec ! [ ] ,
421423 }
422424 }
423425
@@ -588,6 +590,22 @@ impl CxxQtBuilder {
588590 self
589591 }
590592
593+ /// Specify a C++ header and source file containing a Q_OBJECT macro to run [moc](https://doc.qt.io/qt-6/moc.html) on.
594+ /// Unlike [CxxQtBuilder::qobject_header], it includes the generated metatypes.json, so that C++ classes can be included
595+ /// in QML modules.
596+ pub fn qml_qobject (
597+ mut self ,
598+ qobject_header : impl AsRef < Path > ,
599+ qobject_source : impl AsRef < Path > ,
600+ ) -> Self {
601+ let qobject_header = qobject_header. as_ref ( ) . to_path_buf ( ) ;
602+ let qobject_source = qobject_source. as_ref ( ) . to_path_buf ( ) ;
603+ println ! ( "cargo::rerun-if-changed={}" , qobject_header. display( ) ) ;
604+ println ! ( "cargo::rerun-if-changed={}" , qobject_source. display( ) ) ;
605+ self . qml_qobjects . push ( ( qobject_header, qobject_source) ) ;
606+ self
607+ }
608+
591609 /// Use a closure to run additional customization on [CxxQtBuilder]'s internal [cc::Build]
592610 /// before calling [CxxQtBuilder::build]. This allows to add extra include paths, compiler flags,
593611 /// or anything else available via [cc::Build]'s API. For example, to add an include path for
@@ -869,6 +887,22 @@ impl CxxQtBuilder {
869887 }
870888 }
871889
890+ for ( qobject_header, qobject_source) in & self . qml_qobjects {
891+ cc_builder. file ( qobject_source) ;
892+ if let Some ( dir) = qobject_header. parent ( ) {
893+ moc_include_paths. insert ( dir. to_path_buf ( ) ) ;
894+ }
895+ let moc_products = qtbuild. moc (
896+ qobject_header,
897+ MocArguments :: default ( ) . uri ( qml_module. uri . clone ( ) ) ,
898+ ) ;
899+ if let Some ( dir) = moc_products. cpp . parent ( ) {
900+ moc_include_paths. insert ( dir. to_path_buf ( ) ) ;
901+ }
902+ cc_builder. file ( moc_products. cpp ) ;
903+ qml_metatypes_json. push ( moc_products. metatypes_json ) ;
904+ }
905+
872906 let qml_module_registration_files = qtbuild. register_qml_module (
873907 & qml_metatypes_json,
874908 & qml_module. uri ,
0 commit comments