将Qtwiddgets与Qmake一起使用

Using Qtwidgets with Qmake

本文关键字:一起 Qmake Qtwiddgets      更新时间:2023-10-16

我正在使用qmake -projectqmake Prog.pro构建我的Qt应用程序。

当我运行Makefile时,我得到了这个错误:

mainwindow.h:11:21: fatal error: QtWidgets: No such file or directory
 #include <QtWidgets>

我必须添加行

QT += widgets

到.pro文件进行正确编译。有没有办法自动做到这一点?

有没有办法自动做到这一点?

是和否

你可以这样运行qmake:

qmake -project "QT+=widgets"

然后它被正确地生成,但除了从命令行生成之外,没有。此外,请注意,如果你计划支持Qt4,你也希望使用一个保护:

greaterThan(QT_MAJOR_VERSION, 4):QT+=widgets

如果您碰巧使用QtCreator,您将能够避免这一切,因为IDE将自动为您生成这一切。

此外,您应该能够避免将整个模块包含在所有类中,即使您没有全部使用它们。所以,与其写这个:

#include <QtWidgets>

你可以写:

#include <Foo>
#include <Bar>
...