2

I know this is a daft question, but I'm a beginner in visual studio/c++/cmake. I'm looking for a quick intro on how to use Qt5 installed via vcpk using:

vcpkg install qt5-base:x64-windows

This all installed ok and I got the following:

The package qt5-base:x64-windows provides CMake targets:

find_package(Qt5Concurrent CONFIG REQUIRED)
target_link_libraries(main PRIVATE Qt5::Concurrent Qt5::ConcurrentPrivate)

etc....

I just don't know what to do next! Before using libs in VS I just did an <#include> now I'm confronted with this lot... Pref. I want some sort of explanation at newbie level please.

If I add the line (at the top of a .cpp file just as a test):

#include <QtWidgets/QApplication>

It gives: Error (active) E1696 cannot open source file "QtWidgets/QApplication"

I'm new, I thought vcpkg took all the pain out of having to add all the libs etc to the project options? What do I need to do?

Jonn Doe
  • 31
  • 1
  • 1
  • 6
  • 1
    So, what is the problem exactly? Once your executable is linked (as specified here: `target_link_libraries(main PRIVATE Qt5::Concurrent`) just run it. On Windows it might be needed to run `qtdeploy` if used shared libs. – pptaszni Apr 05 '20 at 19:30

2 Answers2

3

If you ran vcpkg integrate install and are just using VS you can just #include <Qt5/QtWidgets/QApplication>

If you are using CMake: find_package(Qt5 COMPONENTS Widgets Concurrent CONFIG REQUIRED) and use target_link_libraries as described by the other answers. But you probably have to switch to #include <QApplication> since cmake file add the QtWidgets folder to the include folders.

For find_package to find the vcpkg build versions you have to specify the vcpkg.cmake toolchain file as the CMAKE_TOOLCHAIN_FILE=<vcpkgroot>/scripts/buildsystems/vcpkg.cmake (must be set in the first CMake call or rather early in the CMakeLists.txt before any project() call) and maybe also VCPKG_TARGET_TRIPLET=<sometriplet> (must also be defined early before CMAKE_TOOLCHAIN_FILE is loaded) if you installed Qt5 using one of the static triplets.

Alexander Neumann
  • 1,479
  • 8
  • 17
  • Isn't necessary to install Qt Visual Studio Tools (https://download.qt.io/archive/vsaddin/2.6.0/)? I mean when not using CMake. I guess that is necessary in order for the Qt projects to become available in VS, file, new project... – KcFnMi Jan 12 '21 at 01:24
  • I think it is only really necessary if you don't want to manually setup the include folder and if you need to moc/uic but even that can be setup manually. My advice is to always use cmake for Qt. – Alexander Neumann Jan 13 '21 at 20:53
0
  1. vcpkg is a cross-platform C++ package manager. Unlike winget, apt, and brew, vcpkg is designed for developers. For example, it builds the binaries from the source by default.

So it help user to have libraries installed in their projects and be able to find them. You still need to learn how CMake canonical find_package() work IMHO.

  1. Qt provide a cmake config package and usually, you'll need to use
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)

then

target_link_libraries(main PRIVATE ... Qt5::Core Qt5::Gui Qt5::Widgets)

ie rule of Thumb: you need a QtWidget/* include ? then target_link to Qt5::Widget etc...

  1. Please note that CMake also provides (i.e. built-in) a few tools to ease Qt-related dev...
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

-> you should try to read the CMake doc...

Amin Ya
  • 1,515
  • 1
  • 19
  • 30
Mizux
  • 8,222
  • 7
  • 32
  • 48