How to use QML_IMPORT_PATH with Qt Cmake Project?

With the new and upcoming QtCreator 4.1 you will be able to do that. Just set QML_IMPORT_PATH in your CMake cache. If you have multiple paths, separate them with a ; which is just how a list is done in 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")

Here is an improvement of @Tom Deblauwe's answer that allows to keep system-specific local settings out of the repository's makefile. It assumes you use QT Creator as your 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.

Tags:

C++

Qt