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

落爺英雄遲暮 提交于 2019-12-21 12:07:22

问题


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.


回答1:


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.




回答2:


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.




回答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



回答4:


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).



来源:https://stackoverflow.com/questions/24669417/qt-5-3-qtwidgets-no-such-file-or-directory-include-qtwidgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!