10

I want to compile Qt example. I get error QtWidgets: No such file or directory #include

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - does not help
QT += widgets                                   - does not help
INCLUDEPATH += /opt/Qt/5.3/Src/qtbase/include/  - does not help

Qt 5.3. Ubuntu 14.04 x64.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Ufx
  • 2,595
  • 12
  • 44
  • 83

4 Answers4

10

You need to double check that you completed all these steps:

  • Module installed

  • greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

  • You re-run the Qt 5 qmake.

Having said that, I would like to remind you that including the whole module is not a good idea as it includes all the widgets related things. Try to narrow it down to the headers that you really need.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Narrowing it down is working. But I want to learn how compile applications which have to compile.-Module installed and -You re-run the Qt 5 qmake. How can I check it? – Ufx Jul 10 '14 at 06:26
  • @Ufx: run /opt/Qt/5.3/Src/qtbase/bin/qmake ... or just click on the corresponding context menu option in QtCreator. – László Papp Jul 10 '14 at 06:38
  • There is remain to check 'Module installed'. How can I check it? – Ufx Jul 10 '14 at 06:43
  • The directory exists. – Ufx Jul 10 '14 at 07:04
  • It is difficult to explain because you are lacking the basics. No, you should not check /opt/Qt/5.3/Src/qtbase/include itself, but whether it has the QtWidgets directory in there... – László Papp Jul 10 '14 at 09:49
9

As you noticed Qt directory structure changed between Qt4 and Qt5. QWidget header moved to a QtWidgets directory. Try adding

INCLUDEPATH += /opt/Qt/5.3/Src/qtbase/include/QtWidgets

If that does not help try finding the header manually using

find /opt/Qt/5.3/Src/qtbase/ -name QWidget

and and the directory it is in to INCLUDEPATH

Edit based on comment from Final Contest.

I agree that workarounds usually are a bad idea. To test where QT your installation looks for qt5 headers and libraries. Create a minimal project.

#include <QApplication>
#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget w;
    w.show();
    app.exec();
}

Generate a project and add QT += widget

/opt/Qt/5.3/Src/qtbase/bin/qmake -project

Project file

######################################################################
# Automatically generated by qmake (3.0) Thu Jul 10 13:05:17 2014
######################################################################

TEMPLATE = app
TARGET = so_qtwidgets
INCLUDEPATH += .

QT += widgets

# Input
SOURCES += main.cpp

Generate a make file

/opt/Qt/5.3/Src/qtbase/bin/qmake

The interesting parts widget flag adds:

  • In my case -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui to INCPATH
  • -DQT_WIDGETS_LIB to DEFINES variable.
  • -lQt5Widgets -lQt5Gui to libs.

The only part which should differ is the paths to QtWidgets and QtGui. If these a wrong the I would try reinstalling Qt.

Tobias
  • 835
  • 1
  • 6
  • 11
  • It helps with that error. But now is new error: QtWidgets/QtWidgetsDepends: No such file or directory #include . I think I understood what do I need to do. But why are official examples need in editing? May be there is way to include all qt directories in the environment settings? – Ufx Jul 10 '14 at 08:37
  • Are you certain /opt/Qt/5.3/Src/qtbase points to the installed qt library? I would not expect Src in that path. On fedora 20 the path is /usr/include/qt5/QtWidgets/. I don't know why there are bugs in Qt's documentation. This is how I solved it. – Tobias Jul 10 '14 at 08:46
  • 2
    It is a bad idea to specify the include path explicitly. `QT+=widgets` exists for this reason. Furthermore, the OP is not using `QWidget header`, but `QtWidgets`. I think you are confused. Furthermore, even if you do that manual include workaround, you will still have the aforementioned problem which would lead to include another path, etc. – László Papp Jul 10 '14 at 09:50
  • Added a minimal widget application to help check qt installation. – Tobias Jul 10 '14 at 12:22
3

Check what your .pro file looks like before you run "make". I found that the command "qmake -project" auto generated a .pro file that caused this same error. I now compiled my qt project via the following commands and the error went away:

qmake my_project.pro
make
Colin McGovern
  • 105
  • 1
  • 6
-1

This all looks very much like the wrong way round and I did have the same problem temporarily with 5.6 but the answer could be a whole lot simpler.

If you're loading a lot of examples you may arrive at the editor or whatever you were at last, first. If the example's been loaded for the first time it'll need to be 'configured' which is under the projects side-tab which should present you with 'Configure' rather than 'Build & Run'. That it doesn't always jump straight there is a flaw, but then so's the inclusion of examples with no support by default (Desktop OpenGL and iOS for two).

Until that's done it'll not resolve any dependencies outside the immediate project as the libraries used depend on which compiler/target is used (eg. MSVS, GNUCC, MinGW, 32/64bit).