@@ -377,6 +377,7 @@ pub struct CxxQtBuilder {
377
377
include_prefix : String ,
378
378
crate_include_root : Option < String > ,
379
379
additional_include_dirs : Vec < PathBuf > ,
380
+ qml_qobjects : Vec < ( PathBuf , PathBuf ) > ,
380
381
}
381
382
382
383
impl CxxQtBuilder {
@@ -418,6 +419,7 @@ impl CxxQtBuilder {
418
419
include_prefix : crate_name ( ) ,
419
420
crate_include_root : Some ( String :: new ( ) ) ,
420
421
additional_include_dirs : vec ! [ ] ,
422
+ qml_qobjects : vec ! [ ] ,
421
423
}
422
424
}
423
425
@@ -588,6 +590,22 @@ impl CxxQtBuilder {
588
590
self
589
591
}
590
592
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
+
591
609
/// Use a closure to run additional customization on [CxxQtBuilder]'s internal [cc::Build]
592
610
/// before calling [CxxQtBuilder::build]. This allows to add extra include paths, compiler flags,
593
611
/// or anything else available via [cc::Build]'s API. For example, to add an include path for
@@ -869,6 +887,22 @@ impl CxxQtBuilder {
869
887
}
870
888
}
871
889
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
+
872
906
let qml_module_registration_files = qtbuild. register_qml_module (
873
907
& qml_metatypes_json,
874
908
& qml_module. uri ,
0 commit comments