Skip to content

Commit 560b0fa

Browse files
committed
cxx-qt-gen: include QQmlEngine when using declarative macros
Otherwise moc does not understand their expansion. Beforw we were manually using the expansion via Q_CLASSINFO but the macros have other code so this resolves the original problem.
1 parent ae4ba67 commit 560b0fa

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

crates/cxx-qt-gen/src/generator/cpp/qobject.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,24 @@ impl GeneratedCppQObjectBlocks {
6262

6363
pub fn from(qobject: &ParsedQObject) -> GeneratedCppQObjectBlocks {
6464
let mut qml_specifiers = Vec::new();
65+
let mut includes = BTreeSet::new();
6566
if let Some(qml_metadata) = &qobject.qml_metadata {
66-
// Somehow moc doesn't include the info in metatypes.json that qmltyperegistrar needs
67-
// when using the QML_ELEMENT/QML_NAMED_ELEMENT macros, but moc works when using what
68-
// those macros expand to.
69-
qml_specifiers.push(format!(
70-
"Q_CLASSINFO(\"QML.Element\", \"{}\")",
71-
qml_metadata.name
72-
));
67+
// When specifying the QML declarative macros moc needs the header to be
68+
// included, otherwise it does not understand what these expand to.
69+
includes.insert("#include <QtQml/QQmlEngine>".to_string());
70+
71+
qml_specifiers.push(format!("QML_NAMED_ELEMENT({})", qml_metadata.name));
7372

7473
if qml_metadata.uncreatable {
75-
qml_specifiers.push("Q_CLASSINFO(\"QML.Creatable\", \"false\")".to_owned());
74+
qml_specifiers.push("QML_UNCREATABLE(\"Not creatable\")".to_owned());
7675
}
7776

7877
if qml_metadata.singleton {
7978
qml_specifiers.push("QML_SINGLETON".to_owned());
8079
}
8180
}
8281
GeneratedCppQObjectBlocks {
82+
includes,
8383
metaobjects: qml_specifiers,
8484
..Default::default()
8585
}
@@ -302,10 +302,7 @@ mod tests {
302302
.unwrap();
303303
assert_eq!(cpp.name.cxx_unqualified(), "MyNamedObject");
304304
assert_eq!(cpp.blocks.metaobjects.len(), 1);
305-
assert_eq!(
306-
cpp.blocks.metaobjects[0],
307-
"Q_CLASSINFO(\"QML.Element\", \"MyQmlElement\")"
308-
);
305+
assert_eq!(cpp.blocks.metaobjects[0], "QML_NAMED_ELEMENT(MyQmlElement)");
309306
}
310307

311308
#[test]
@@ -322,10 +319,7 @@ mod tests {
322319
.unwrap();
323320
assert_eq!(cpp.name.cxx_unqualified(), "MyObject");
324321
assert_eq!(cpp.blocks.metaobjects.len(), 2);
325-
assert_eq!(
326-
cpp.blocks.metaobjects[0],
327-
"Q_CLASSINFO(\"QML.Element\", \"MyObject\")"
328-
);
322+
assert_eq!(cpp.blocks.metaobjects[0], "QML_NAMED_ELEMENT(MyObject)");
329323
assert_eq!(cpp.blocks.metaobjects[1], "QML_SINGLETON");
330324
}
331325

@@ -353,13 +347,10 @@ mod tests {
353347
.unwrap();
354348
assert_eq!(cpp.name.cxx_unqualified(), "MyObject");
355349
assert_eq!(cpp.blocks.metaobjects.len(), 2);
356-
assert_eq!(
357-
cpp.blocks.metaobjects[0],
358-
"Q_CLASSINFO(\"QML.Element\", \"MyObject\")"
359-
);
350+
assert_eq!(cpp.blocks.metaobjects[0], "QML_NAMED_ELEMENT(MyObject)");
360351
assert_eq!(
361352
cpp.blocks.metaobjects[1],
362-
"Q_CLASSINFO(\"QML.Creatable\", \"false\")"
353+
"QML_UNCREATABLE(\"Not creatable\")"
363354
);
364355
}
365356
}

0 commit comments

Comments
 (0)