What is the correct way to include Qt headers?

So far, I know several ways #includeof Qt classes:

  • #include <QtModule>

    It brings all the classes of a specific module, for example QDomDocument, QDomElement, QDomNodeand many others from #include <QtXml>.

  • #include <QClassName>

    This adds the declaration of a specific class that can be used, for example. QEvent, QStringList, QFile.

  • #include <qcstyleheader.h>

    This affects the previous method, with the exception of a different kind of header.

So, I wonder if there are other ways for #includeQt classes? Are they equivalent or do some prefer others for some reason? #includeDoes it depend on -in the file .cppor .h? Does this affect compilation speed and executable file size?

In short, what is the best way to go?

+5
source share
4 answers

Generally, the more header files, the more compiler is needed to parse each module. (Obviously, precompiled headers display some of these problems.) Therefore, you usually want to include the least number of header files needed to properly build your application.

If you use only a few classes in this compiler, just include the classes by name in a modern style:

#include <QEvent>
#include <QPainter>
#include <QFont>

If you use a large number of classes from this module, it is probably also easy to include a header at the module level, for example:

#include <QtGui>

Usually an older style .his used if the new style heading does not exist.

, . - . , , .

+9

. : , . . . , , .

+1

, - ... - , - , , .

, , Qt, , . , #include , .cpp . .h - , #include , . , , , #include "class foo;" . , # , .

+1

Personally, I found significant advantages (~ 30% of compilation time?) From all included Qt, which I use in a precompiled header ( example ), and not in each .h / .cpp as needed. Of course, a minor flaw is that you may lose sight of which bits of Qt depend on your individual source files, but I have not found that this is the problem itself.

+1
source

All Articles