如何在Qt Cmake项目中使用QML_IMPORT_PATH?

13

我需要为项目提供一些模块。

目前,它会在QT目录中查找它们(我已经将其安装在$HOME中),但我希望让它在/usr/lib/x86_64-linux-gnu/qt5/qml/中进行搜索。

我尝试过:
a).bashrc中定义QML_IMPORT_PATH - 没有成功

b) 将所需的模块复制到$HOME/Qt5.5.1/Tools/QtCreator/bin/qml/中:

这里有些不同。如果我打开.qml文件,它不会划掉import行(这很好)。但是,如果我通过控制台运行可执行文件,它会显示相同的消息module org.bla.bla is not installed

因此,如果复制没有帮助,也许我只需要让QtCreator(或其他东西)在适当的文件夹中搜索模块,但是如何实现呢?

更新。
嗯,您可以使用QML2_IMPORT_PATH(不仅是QML,还包括QML2)定义模块路径。如我上面提到的,我已将模块文件夹复制到$HOME/Qt5.5.1/Tools/QtCreator/bin/qml/中,这是完全错误的!正确的方法是将其复制到$QT_HOME/5.5/gcc_64/qml/中。它现在可以正常运行,但我不能说它是如何工作的。不幸的是,这与我提出的问题无关。因此,我不会要求其他人回答我的问题,但也不会关闭它,直到找到真正的问题并在此处提及,以便我可以帮助其他人。

2个回答

17

通过新的QtCreator 4.1,您将能够做到这一点。只需在您的CMake缓存中设置QML_IMPORT_PATH。如果您有多个路径,请使用;分隔它们,这就是CMake中列表的方式。

list(APPEND QML_DIRS "dir1")
list(APPEND QML_DIRS "dir2")
set(QML_IMPORT_PATH "${QML_DIRS}" CACHE STRING "Qt Creator 4.1 extra qml import paths")

谢谢。我会考虑的,看起来可以将这个问题标记为答案。 - pushandpop
在 CMake 运行后,此设置将被缓存在 ${BUILD_DIR}/CMakeCache.txt 中。如果您需要在以后更改它,请删除该文件。(或者删除整个构建目录内容,相当于 CMake 中的 make clean。) - tanius
如果您使用路径,还可以创建“CACHE PATH”。 - ניר

7

这里是 @Tom Deblauwe 的答案改进版,允许将系统特定的本地设置从存储库的 makefile 中分离出来。它假定您使用 QT Creator 作为您的 IDE。

  1. In Qt Creator, open the "Projects" sidebar tab and there go to "Build & Run → [your build config's entry] → Build → CMake".

  2. In the list of CMake configuration settings you find there, set the value of setting QML_IMPORT_PATH according to your system. Separate multiple directories with ";".

  3. You can additionally provide some common defaults in the repository's CMakeLists.txt makefile so that users with common setups don't need to set their their QML_IMPORT_PATH. The code below will not overwrite the user's QML_IMPORT_PATH but append to it. You'd add the following to CMakeLists.txt:

    # Directories where Qt Creator can find QML files.
    # (Not needed for builds, but makes Qt Creator code completion happy.)
    list(APPEND QML_IMPORT_PATH "/example/path/to/qml")
    list(APPEND QML_IMPORT_PATH "/second/example/path/to/qml")
    
    # Prevent adding duplicate values at each run of CMake.
    list(REMOVE_DUPLICATES QML_IMPORT_PATH)
    
    # The variable is cached in ${BUILD_DIR}/CMakeCache.txt. We need FORCE to 
    # change it there immediately. Also, add a comment to the cache file.
    set(QML_IMPORT_PATH ${QML_IMPORT_PATH}
        CACHE STRING "Qt Creator 4.1 extra qml import paths"
        FORCE
    )
    
  4. After running CMake, QML_IMPORT_PATH is now the user-defined value plus some CMakeLists.txt defined values appended to it. Qt Creator's CMake configuration settings from steps 1-2 will still show the user's value in the table. But when mousing over that value, the full value incl. the appended portion is shown in a popover.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接