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

在qt_add_qml_module所在目录的子文件夹中使用qt_target_qml_sources构建失败的问题求助

在qt_add_qml_module所在目录的子文件夹中使用qt_target_qml_sources构建失败的问题求助

大家好,最近我在搭建Qt项目时遇到了一个构建问题,想请各位帮忙看看。

我的需求是把QML源码拆分到子文件夹中,每个子文件夹用独立的CMakeLists.txt管理——主模块用qt_add_qml_module创建,子文件夹里用qt_target_qml_sources往主模块里添加QML文件,但编译时总是失败。

我的CMake配置

首先是view文件夹下的主CMakeLists.txt:

qt_add_library(MainModule STATIC)
qt_add_qml_module(MainModule
    URI main_module
    VERSION 1.0
    QML_FILES
        Main_Page.qml
)
add_subdirectory(submodule)

然后是submodule子文件夹里的CMakeLists.txt:

qt_target_qml_sources(MainModule
    QML_FILES
        Sub_Page.qml
)

报错信息

用GCC编译器构建时,出现了以下错误:

<project_folder>\modularMinQml\min_qml_module\build\Desktop_Qt_6_6_3_MinGW_64_bit-Debug\view\submodule.rcc\qmlcache\MainModule_Sub_Page_qml.cpp:-1: error: <project_folder>/modularMinQml/min_qml_module/build/Desktop_Qt_6_6_3_MinGW_64_bit-Debug/view/submodule/.rcc/qmlcache/MainModule_Sub_Page_qml.cpp: No such file or directory

补充说明

值得一提的是,如果我把qt_target_qml_sources的代码直接写在主CMakeLists.txt里(和qt_add_qml_module同文件),项目就能正常编译运行,只有拆分到子文件夹的CMakeLists时才会触发这个错误。

备注:内容来源于stack exchange,提问作者morteza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.16 10:48:03