@@ -358,6 +358,7 @@ pub struct CxxQtBuilder {
358
358
public_interface : Option < Interface > ,
359
359
include_prefix : String ,
360
360
initializers : Vec < String > ,
361
+ qml_qobjects : Vec < ( PathBuf , PathBuf ) > ,
361
362
}
362
363
363
364
impl CxxQtBuilder {
@@ -398,6 +399,7 @@ impl CxxQtBuilder {
398
399
initializers : vec ! [ ] ,
399
400
public_interface : None ,
400
401
include_prefix : crate_name ( ) ,
402
+ qml_qobjects : vec ! [ ] ,
401
403
}
402
404
}
403
405
@@ -532,6 +534,22 @@ impl CxxQtBuilder {
532
534
self
533
535
}
534
536
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
+
535
553
/// Use a closure to run additional customization on [CxxQtBuilder]'s internal [cc::Build]
536
554
/// before calling [CxxQtBuilder::build]. This allows to add extra include paths, compiler flags,
537
555
/// or anything else available via [cc::Build]'s API. For example, to add an include path for
@@ -844,6 +862,22 @@ impl CxxQtBuilder {
844
862
}
845
863
}
846
864
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
+
847
881
let qml_module_registration_files = qtbuild. register_qml_module (
848
882
& qml_metatypes_json,
849
883
& qml_module. uri ,
0 commit comments