Skip to content

Commit c415678

Browse files
authored
cxx-qt-build: inject QML_FOREIGN types for qmllint/qmlls (#1344)
This allows them to understand the std numerics when namespaced as Qt only registers int32_t not ::std::int32_t so qmllint and qmlls state the type is not resolved.
1 parent 2ae1b85 commit c415678

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

crates/cxx-qt-build/cpp/builtins.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3+
// clang-format on
4+
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
8+
#pragma once
9+
10+
#include <cstdint>
11+
12+
#include <QtQml/QQmlEngine>
13+
14+
// This is similar to the builtins file in qtdeclarative
15+
// https://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/qml/qqmlbuiltins_p.h?h=v6.9.3
16+
//
17+
// We need this to be able to alias namespaced std numerics to types that QML
18+
// understands.
19+
//
20+
// We do not need to build this file but the moc JSON output to give to
21+
// qmltyperegistrar so that qmllint and qmlls can understand the types.
22+
//
23+
// If Qt ever registered qualified versions of the numerics this could be
24+
// removed.
25+
//
26+
// qqmlbuiltins uses the following values for QML_USING so we should copy
27+
// i8, u8 -> qint8, quint8
28+
// i16, u16 -> short, ushort
29+
// i32, u32 -> int, uint
30+
// i64, u64 -> qlonglong, qulonglong
31+
32+
struct QQmlCxxQtStdInt8TForeign
33+
{
34+
Q_GADGET
35+
QML_FOREIGN(::std::int8_t)
36+
QML_USING(qint8)
37+
};
38+
static_assert(sizeof(::std::int8_t) == sizeof(qint8));
39+
40+
struct QQmlCxxQtStdUInt8TForeign
41+
{
42+
Q_GADGET
43+
QML_FOREIGN(::std::uint8_t)
44+
QML_USING(quint8)
45+
};
46+
static_assert(sizeof(::std::uint8_t) == sizeof(quint8));
47+
48+
struct QQmlCxxQtStdInt16TForeign
49+
{
50+
Q_GADGET
51+
QML_FOREIGN(::std::int16_t)
52+
QML_USING(short)
53+
};
54+
static_assert(sizeof(::std::int16_t) == sizeof(short));
55+
56+
struct QQmlCxxQtStdUInt16TForeign
57+
{
58+
Q_GADGET
59+
QML_FOREIGN(::std::uint16_t)
60+
QML_USING(ushort)
61+
};
62+
static_assert(sizeof(::std::uint16_t) == sizeof(ushort));
63+
64+
struct QQmlCxxQtStdInt32TForeign
65+
{
66+
Q_GADGET
67+
QML_FOREIGN(::std::int32_t)
68+
QML_USING(int)
69+
};
70+
static_assert(sizeof(::std::int32_t) == sizeof(int));
71+
72+
struct QQmlCxxQtStdUInt32TForeign
73+
{
74+
Q_GADGET
75+
QML_FOREIGN(::std::uint32_t)
76+
QML_USING(uint)
77+
};
78+
static_assert(sizeof(::std::uint32_t) == sizeof(uint));
79+
80+
struct QQmlCxxQtStdInt64TForeign
81+
{
82+
Q_GADGET
83+
QML_FOREIGN(::std::int64_t)
84+
QML_USING(qlonglong)
85+
};
86+
static_assert(sizeof(::std::int64_t) == sizeof(qlonglong));
87+
88+
struct QQmlCxxQtStdUInt64TForeign
89+
{
90+
Q_GADGET
91+
QML_FOREIGN(::std::uint64_t)
92+
QML_USING(qulonglong)
93+
};
94+
static_assert(sizeof(::std::uint64_t) == sizeof(qulonglong));

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,22 @@ impl CxxQtBuilder {
895895
let cc_builder = &mut self.cc_builder;
896896
qtbuild.cargo_link_libraries(cc_builder);
897897

898-
let qml_metatypes_json: Vec<PathBuf> = moc_products
898+
let mut qml_metatypes_json: Vec<PathBuf> = moc_products
899899
.iter()
900900
.map(|products| products.metatypes_json.clone())
901901
.collect();
902902

903+
// Inject CXX-Qt builtin meta types
904+
let builtins_path = dir::out().join("builtins.h");
905+
std::fs::write(&builtins_path, include_str!("../cpp/builtins.h"))
906+
.expect("Failed to write builtins.h");
907+
qml_metatypes_json.push(
908+
qtbuild
909+
.moc()
910+
.compile(builtins_path, MocArguments::default())
911+
.metatypes_json,
912+
);
913+
903914
let qml_module_registration_files = qtbuild.register_qml_module(
904915
&qml_metatypes_json,
905916
&qml_module.uri,

0 commit comments

Comments
 (0)