Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 5.3. QtWidgets: No such file or directory #include <QtWidgets>

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.

like image 748
Ufx Avatar asked Jul 10 '14 05:07

Ufx


3 Answers

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.

like image 55
lpapp Avatar answered Nov 10 '22 18:11

lpapp


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.

like image 42
Tobias Avatar answered Nov 10 '22 17:11

Tobias


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
like image 3
Colin McGovern Avatar answered Nov 10 '22 18:11

Colin McGovern