未找到自定义 QML 模块

Sil*_*lex 3 qt qml qt5

我有一个 QML 应用程序。我创建了自己的 QML 模块。叫MyCustomModule. 模块有相应的qmldir文件,注册到相应的my_custom_module.qrc文件中。我还在 C++ 中的应用程序启动时添加了导入路径addImportPath("qrc:///my_custom_module");。我正在使用 CMake 而不是 QMake。

无论我在哪里导入MyCustomModuleQtCreator 都会告诉我QML module not found,但是当我构建应用程序时,构建没有任何问题并运行。

我错过了什么吗?

Sil*_*lex 5

我的问题是QML_IMPORT_PATH我的 CMake 文件丢失了。例子:

# Make Qt Creator aware of where the QML modules live
set (_QML_IMPORT_PATHS "")

## Add new module paths here.
list (APPEND _QML_IMPORT_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/path/to/your/module)

set (
    QML_IMPORT_PATH
    ${_QML_IMPORT_PATHS}
    CACHE
    STRING
    "Path used to locate CMake modules by Qt Creator"
    FORCE
)
Run Code Online (Sandbox Code Playgroud)

一个重要的旁注是,${CMAKE_CURRENT_SOURCE_DIR}/path/to/your/module应该指向模块所在的文件夹,而不是模块文件夹本身。因此,如果您有这样的路径: /path/to/your/module/MyCustomModule,CMake 应包含该路径/path/to/your/module