You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt6项目编译报错:qt6apptestqt_debug_metatypes.json非法值

问题:Qt 6.6.0编译报错:解析空的qt6apptestqt_debug_metatypes.json提示非法值

使用Qt 6.6.0搭建测试项目,编译时出现错误:解析qt6apptestqt_debug_metatypes.json时提示非法值,且该文件为空。项目代码及错误日志如下:

CMake文件

cmake_minimum_required(VERSION 3.16)

project(testqt VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)

qt_standard_project_setup()

qt_add_executable(apptestqt
    main.cpp test.cpp test.hpp
)

qt_add_qml_module(apptestqt
    URI testqt
    VERSION 1.0
    QML_FILES Main.qml
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(apptestqt PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.apptestqt
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(apptestqt
    PRIVATE Qt6::Quick
)

include(GNUInstallDirs)
install(TARGETS apptestqt
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

头文件test.hpp

#pragma once
#include <QQmlEngine>

class test_gadget {
    Q_GADGET
    QML_ELEMENT
    Q_PROPERTY(int test MEMBER test)
public:
    int test;
};

class test_object : public QObject {
    Q_OBJECT
    QML_ELEMENT
public:
    test_object(const test_gadget&);
private:
    test_gadget gadget;
};

源文件test.cpp

#include <test.hpp>

test_object::test_object(const test_gadget& other) {
    this->gadget = other;
}

主文件main.cpp

#include <QQmlContext>
#include <test.hpp>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    const QUrl url(u"qrc:/testqt/Main.qml"_qs);
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
        &app, []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    test_gadget gadget;
    test_object test_obj(gadget);
    engine.rootContext()->setContextProperty("test_obj", &test_obj);
    engine.load(url);

    return app.exec();
}

编译错误日志

[  5%] Built target apptestqt_qmlimportscan
[ 10%] Built target apptestqt_tooling
[ 15%] Automatic MOC and UIC for target apptestqt
[ 20%] Built target apptestqt_autogen
[ 25%] Running AUTOMOC file extraction for target apptestqt
[ 25%] Built target apptestqt_automoc_json_extraction
[ 30%] Automatic QML type registration for target apptestqt
Error 5 while parsing C:/Users/USER/source/repos/build-testqt-Desktop_Qt_6_6_0_MinGW_64_bit-Debug/meta_types/qt6apptestqt_debug_metatypes.json: illegal value
mingw32-make.exe[2]: *** [CMakeFiles\apptestqt.dir\build.make:88: apptestqt_qmltyperegistrations.cpp] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:97: CMakeFiles/apptestqt.dir/all] Error 2
mingw32-make.exe: *** [Makefile:135: all] Error 2
13:22:22: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 2.
Error while building/deploying project testqt (kit: Desktop Qt 6.6.0 MinGW 64-bit)
When executing step "Build"

注意:build-testqt-Desktop_Qt_6_6_0_MinGW_64_bit-Debug\meta_types\qt6apptestqt_debug_metatypes.json文件为空。


解决方案

问题原因

错误根源是**test_object类缺少默认构造函数**。带有QML_ELEMENT标记的QObject子类,Qt的MOC/QML类型注册系统要求必须提供无参默认构造函数,否则元类型生成流程会失败,最终生成空的metatypes.json文件,触发解析错误。

修复步骤

  1. 给test_object添加默认构造函数
    修改test.hpp中的类声明,添加无参构造函数的声明:

    class test_object : public QObject {
        Q_OBJECT
        QML_ELEMENT
    public:
        test_object(); // 添加默认构造函数声明
        test_object(const test_gadget&);
    private:
        test_gadget gadget;
    };
    

    然后在test.cpp中实现该构造函数:

    test_object::test_object() {}
    
  2. 清理并重新构建项目

    • 删除整个build目录,避免残留的错误文件干扰编译流程
    • 重新运行CMake配置,再执行编译操作

额外说明

  • 带有QML_ELEMENT的QObject子类必须满足QML类型系统的要求,默认构造函数是必填项,因为QML引擎可能需要直接实例化该类
  • 如果业务上不需要公开默认构造函数,可以将其声明为private,但仍需提供实现供Qt内部使用,不过更推荐保持public以便后续扩展

内容的提问来源于stack exchange,提问作者user1558325

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 23:01:00