C++ Qt 5.3。QtWidgets:没有这样的文件或目录#include <QtWidgets>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24669417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:47:37  来源:igfitidea点击:

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

c++qtqt5qmakeqtwidgets

提问by Ufx

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

我想编译 Qt 示例。我收到错误 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.

Qt 5.3。Ubuntu 14.04 x64。

回答by lpapp

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.

  • 已安装模块

  • GreaterThan(QT_MAJOR_VERSION, 4):QT += 小部件

  • 你重新运行了 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.

话虽如此,我想提醒您,包括整个模块并不是一个好主意,因为它包括所有与小部件相关的东西。尝试将其缩小到您真正需要的标题。

回答by Tobias

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

正如您所注意到的,Qt 目录结构在 Qt4 和 Qt5 之间发生了变化。QWidget 标头移至 QtWidgets 目录。尝试添加

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

以及它在 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.

我同意变通方法通常是一个坏主意。要测试您的安装在哪里 QT 查找 qt5 头文件和库。创建一个最小的项目。

#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

生成项目并添加 QT += 小部件

/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

生成make文件

/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.
  • 在我的情况下 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui 到 INCPATH
  • -DQT_WIDGETS_LIB 到 DEFINES 变量。
  • -lQt5Widgets -lQt5Gui 到库。

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

唯一应该不同的部分是 QtWidgets 和 QtGui 的路径。如果这些是错误的,我会尝试重新安装 Qt。

回答by Colin Mcgovern

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:

在运行“make”之前检查您的 .pro 文件是什么样的。我发现命令“qmake -project”自动生成了一个导致同样错误的 .pro 文件。我现在通过以下命令编译了我的 qt 项目,错误消失了:

qmake my_project.pro
make

回答by user4503219

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.

这一切看起来很像错误的方式,我确实在 5.6 中暂时遇到了同样的问题,但答案可能要简单得多。

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

如果您正在加载大量示例,您可能会首先到达编辑器或任何您最终所在的位置。如果该示例是第一次加载,则需要在项目侧选项卡下进行“配置”,该选项卡应向您显示“配置”而不是“构建和运行”。它并不总是直接跳转是一个缺陷,但是包含默认情况下不支持的示例也是如此(桌面 OpenGL 和 iOS 两个)。

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

在完成之前,它不会解决直接项目之外的任何依赖项,因为使用的库取决于使用的编译器/目标(例如 MSVS、GNUCC、MinGW、32/64 位)。