質問

I am building my Qt Application with qmake -project and qmake Prog.pro.

When I run the Makefile I get this error:

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

I have to add the line

QT += widgets

To the .pro file to compile correctly. Is there a way to do this automatically?

役に立ちましたか?

解決

Is there a way to do this automatically?

Yes and no.

You could run qmake like this:

qmake -project "QT+=widgets"

and then it is generated properly, but other than that from the command line, no. Also, note that you would like to use a guard if you plan to support Qt 4, too:

greaterThan(QT_MAJOR_VERSION, 4):QT+=widgets

If you happen to use QtCreator, you will be able to avoid all this as the IDE will generate this all for you automatically.

Also, you should be able to avoid including the whole module with all the classes even though you do not use them all. So, instead of writing this:

#include <QtWidgets>

You could write:

#include <Foo>
#include <Bar>
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top