sashoalm
sashoalm

Reputation: 79595

"#include <QtCore/QCoreApplication>" vs "#include <QCoreApplication>"

When I created a new Mobile application in Qt Creator, I noticed that in the autogenerated code they use #include <QtCore/QCoreApplication> instead of #include <QCoreApplication>, even though both should work the same.

Since Qt Creator itself does that, I assume it's the recommended convention. But is there any reason why this is so, can someone explain what is the point to using #include <QtCore/QCoreApplication> instead of #include <QCoreApplication>?

Upvotes: 0

Views: 6787

Answers (2)

Claudio
Claudio

Reputation: 1706

For code generated from forms you can change that in "Designer" tab options: "Use Qt module name in #include-directive".

I noticed one little but sometimes annoying problem when specifying the module. By default qmake add /path/to/qt/sdk/include to include path. This means that you can for example do

#include <QtGui/QWidget>

even if in your project file you have

QT -= gui

Then the compilation goes fine but the linker will be mad at you, since QtGui4 library won't be selected for linking.

If you did

#include <QWidget>

the compilation would instead fail.

Upvotes: 1

perilbrain
perilbrain

Reputation: 8207

There are multiple reason for using such convention :-

  1. If path included in the compiler is at the directory level of QtCore then it will trace the include file QCoreApplication from there.
  2. Since there are multiple version of Qt existing in same directory,it is preferred to use a convention that discards ambiguity.(Since two QCoreApplication one belonging to Qt3 and other Qt4 will mislead compiler)

I have two directories in /usr/include/

  • QtCore
  • Qt

both of them have QCoreApplication in them.

Upvotes: 2

Related Questions