Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QML_IMPORT_PATH with Qt Cmake Project?

Tags:

c++

qt

I need to provide some modules for project.

Now, it looks for them in QT directory(I've installed it in $HOME), but instead of it I want to make it search in /usr/lib/x86_64-linux-gnu/qt5/qml/.

What I have tried:
a) Defining QML_IMPORT_PATH in .bashrc - didn't work out

b) Copying needed module in $HOME/Qt5.5.1/Tools/QtCreator/bin/qml/:

Here we have something different. If I open of the .qml files - it wouldn't underscore import line (which is ok). But, If I run executable with console - the same message module org.bla.bla is not installed.

So, If copying didn't help, maybe I had to just make QtCreator(or smth else) search for modules in appropriate folder, but how?

UPD.
Well, you can define path to your modules with QML2_IMPORT_PATH(not just QML, but QML2). As I mentioned above, I've copied module folder in $HOME/Qt5.5.1/Tools/QtCreator/bin/qml/ which is completely wrong! The right way was to copy it in $QT_HOME/5.5/gcc_64/qml/. It runs now fine, but I can't say the same about "how" it works. Unfortunately, this is not related to the question I've asked. Therefore, I'll not ask others to answer my question, but won't close it as well until find real problem and mention it here, so I can help others.

like image 644
pushandpop Avatar asked Dec 19 '15 11:12

pushandpop


2 Answers

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")
like image 109
Tom Deblauwe Avatar answered Sep 21 '22 18:09

Tom Deblauwe


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.

like image 34
tanius Avatar answered Sep 19 '22 18:09

tanius