11

I am adding QT like this:

find_package(Qt5 COMPONENTS Core Quick REQUIRED)
...
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick ${OpenCV_LIBS})

but cmake finds some python artifacts instead of expected C:\QT...

enter image description here

How can I change this? Tried to find some examples, but didn't find any clear instruction, which subfolder of C:\QT\ I should specify (any where).

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206

3 Answers3

14

find_package search order is following:

  1. Search in cache variables: CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH
  2. Search in environment variables: <package>_DIR, CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH.
  3. Search in the HINTS option.
  4. Search the PATH environment variable.
  5. And in some more "desperate" places. More about that here.

With that in mind there are several ways to provide a proper version to QT:

  1. Have an environment variable pointing to the proper version of QT (e.g. QTDIR). And use it in the CMake files:
    • like set(CMAKE_PREFIX_PATH "$ENV{QTDIR}")
    • or find_package(Qt5 HINTS "$ENV{QTDIR}" COMPONENTS Core Quick REQUIRED)
  2. Have an environment variable explicitly named Qt5_DIR pointing to the proper version of QT. Then no additional changes to CMake files are required.
  3. Make sure that required version of Qt is the first one to be found in the PATH environment variable, for example, for windows C:\Qt\Qt5.10.1\5.10.1\msvc2017_64
zwcloud
  • 4,546
  • 3
  • 40
  • 69
Teivaz
  • 5,462
  • 4
  • 37
  • 75
  • There's a simpler alternative which happens to be the standard approach: set the cmake variables at config time. Just run cmake-gui and set `Qt5_dir` to point to the path where the desired Qt5 version is installed. – RAM Jun 30 '18 at 08:23
  • *Make sure that required version of Qt is the first one to be found in the PATH environment variable.* For example? – zwcloud Jul 24 '19 at 07:49
  • 1
    `C:\Qt\Qt5.10.1` or `C:\Qt\Qt5.10.1\5.10.1\msvc2017_64` or `C:\Qt\Qt5.10.1\5.10.1\msvc2017_64\lib\cmake\Qt5`? – zwcloud Jul 24 '19 at 07:50
  • @zwcloud the “msvc2017_64” is the correct one for windows – Teivaz Jul 24 '19 at 07:51
5

You can set click the Add Entry button in CMake Gui and add a new variable called Qt5_DIR, select its type as PATH and its value to something like C:\Qt\5.11.0\msvc2017_64\lib\cmake\Qt5 where 5.11.0 is the Qt version. This folder must contain Qt5Config.cmake that CMake needs to set things up correctly.

  • Thank you for exactly describing which path to set. I did not know that there are cmake-files hidden somewhere in QT. One thing Windows users should be reminded of is restarting the PC after setting environment variables or else they won't take effect. +1 for you – Philli Feb 15 '19 at 15:40
  • @Philli it will take effect if you launch the app or cmd/powershell via start menu and so on instead of File Explorer. Also in some cases you don't need to save it globally, just adding `SET Qt5_DIR=.....` before executing the command may be enough. – Alex P. Mar 11 '19 at 09:42
2

My workaround was to put desired QT to the top of PATH variable. It has to be in PATH if you want CMAKE to find it.

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206