0
votes

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?

2
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

2 Answers

2
votes

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.

0
votes

1) vcpkg is "just" another C++ libraries package manager since windows don't have a good package manager like any GNU/Linux or BSD distro (or homebrew on macOS). So it help user to have libraries installed on their system and be able to find them. You still need to learn how CMake canonical find_package() work IMHO.

2) IIRC Qt provide 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...

3) Please note CMake also provide (i.e. built-in) 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...