1

I'm trying to link Qt5 in Visual Studio 15 2017.

I have added the paths and file references for additional (1) include directories, (2) library directories and (3) dependencies in the Property Pages.

I can include header files by refering to their path like this

#include <QtWidgets/qpushbutton.h>

However, the github project I'm trying to build uses this line

#include <QtWidgets>

Visual Studio shows me an error on those lines

E1696   cannot open source file "QtWidgets" 

When creating a solution from a .pro file using the Qt VS Tools in Visual Studio, I can include <QtWidgets>. But how can I include it in my existing project, which has not been created from a .pro file.


Solution Placing a MyProjectName.pro file (with QT += core gui widgets) in the MyProjectName Folder and opening it with Qt VS Tools > Open Qt Project File (.pro) solves this problem. I can now include <QtWidgets>. But this doesn't seem like the correct way, given the VS gui.

Leander
  • 508
  • 6
  • 21
  • 1
    The tutorial you linked is for Qt 4. With Qt 5 you'll need to add [`QT += widgets` to your .pro file](https://stackoverflow.com/questions/24669417/qt-5-3-qtwidgets-no-such-file-or-directory-include-qtwidgets). – gan_ Aug 07 '19 at 12:55
  • Be careful, `#include `, single, without s, not QtWidgets. – 273K Aug 07 '19 at 13:17
  • `#include ` is correct. (Just looked [here](https://stackoverflow.com/a/57158965/7478597) to confirm but have used it for sample codes many times before.) It works in Qt 5 for VS (with CMake project) as well as with Qt project files (if Qt widgets are enabled properly in both cases). – Scheff's Cat Aug 07 '19 at 13:32
  • @S.M. No, `#include ` is fine. There is a 's' because it includes the whole widgets module. – Fareanor Aug 07 '19 at 14:02
  • Why do you think your solution isn't correct? Qt project files are the Qt-ish way to provide some kind of portable build script. On Linux, the qmake tool produces a `Makefile` from it which can be used to build the executable with `make`. (With Qt VS Tools, I haven't any experience. I guess they provide a similar tool to prepare a VS project file out of a Qt project.) A subject of build scripts is the setup of appropriate include and library paths. – Scheff's Cat Aug 07 '19 at 14:14
  • @Scheff I thought that the settings could all be done in the GUI of VS, since it provides a UI for linking libraries and making builds. If you think this is the answer, you can post it as such an I can accept it. – Leander Aug 07 '19 at 14:19
  • It's surely possible to do all the settings manually by UI. However, this might become tedious. I can imagine that Qt VS Tools can configure as well necessary defines and custom build steps (e.g. for Qt MOC). I once maintained VS projects manually (in a text editor) until we introduced CMake. (I wouldn't return ever...) ;-) – Scheff's Cat Aug 07 '19 at 14:22

1 Answers1

4

The "QtWidgets" include file is a file in the "QtWidgets" directory. Your include path probably has something like "$(QTDIR)/include", but not "$(QTDIR)/include/QtWidgets". That's why your example with "QtWidgets/qpushbutton.h" works, but not just "QtWidgets" on its own.

Maybe it helps to look at the full path to the file you're trying to include (using my Qt 5.5.1 installation):

C:\Qt\Qt5.5.1\5.5\msvc2013_64\include\QtWidgets\QtWidgets

So if you have this in your include path:

C:\Qt\Qt5.5.1\5.5\msvc2013_64\include

That only gets you to the QtWidgets directory, but not to the QtWidgets file.

You also needs this in your include path:

C:\Qt\Qt5.5.1\5.5\msvc2013_64\include\QtWidgets

Obviously, adjust this for your installed drive, Qt version, and MSVC version.

goug
  • 2,294
  • 1
  • 11
  • 15