10

While programming in C++ with Qt what includes should I make? Just 2 includes

#include <QtCore>
#include <QtGui>

or

#include <QWidget>
#include <QDialog>
#include <QList>
#include <QKeyEvent>
#include <QObject>

for every class?

Thanks!

4 Answers4

11

Include only the definitions of the classes you need - anything else isn't just lazy, it's extremely wasteful and to my mind bad style.

Including QtGui (which itself includes QtCore) will lead to adding about 350(!) header files to your compilation, when in your example you only needed 6. Compiling will take longer, and when someone tries to maintain your app and is browsing your files they won't be able to infer from just the includes what exactly it is each file/class is trying to do - they'll have to read the entire source to get an idea.

dabhaid
  • 3,849
  • 22
  • 30
  • 3
    All true! One other consideration, use forward declarations where possible in your header files. – Kaleb Pederson May 25 '11 at 19:46
  • 3
    This answer is wrong (partially right) because it does not take into account the "precompiled header" feature, which turns cons into pros. I say that if you use only generic Qt includes, and no separate includes, you will get the decent compilation speed and the smallest binary size. This is assuming you have modern compiler with optimizations on, of cause. Hand-picking includes is a 90-s stuff. Let machine care of obvious stuff, it does that better than man. –  Aug 23 '13 at 09:12
  • @BarafuAlbino Precompiled headers are nice when developing a project, but if the user is, for example, building open-source software once just to use it, all headers will need to be compiled anyways. Hand-picking includes is not "90's stuff". It is just common sense to clearly declare your dependencies in a C++ file. – SirDarius Oct 10 '13 at 08:24
  • 1
    No again. Modern compiler can even skip parts that are written but never used. In gcc, the compiler module will evaluate all headers and subheaders, but this is not a time-consuming action. I will not build them or something - the code is pre-built already. After that, linker will just ignore all unused code. So what you really get is moving some jobs from "per every file" to "PCH preparation" phase. You don't loose much, but save you a problem of distractions to write includes in. Don't put all libs on our PC in PCH, though - sorting these will take time. –  Nov 21 '13 at 07:27
5

Just include what you need in your classes to speed up compiling. If you're one of the lazy guys and don't matter how long it takes to compile, pick the more generic headers but avoid them if you're looking for optimization (the compiler/linker will most likely remove unused stuff but it's still better to not include it first).

In general I only include new stuff if I need something that's still missing (i.e. not included yet). So, just hit compile. If it's missing something you forgot, add the header. If you don't like that approach, use the generic headers you mentioned.

Mario
  • 35,726
  • 5
  • 62
  • 78
2

If you don't want to bother including each individual class then include entire components:

#include <QtCore>
#include <QtGui>

And any other QT component you need to use

#include <QtNetwork>

etc.

If you want to explicitly specify your dependencies then include every individual class

#include <QWidget>
#include <QDialog>
#include <QList>
#include <QKeyEvent>

and other QT classes you are using.

Serge Dundich
  • 4,221
  • 2
  • 21
  • 16
0

The only difference is in compilation time. If you don't use precompile QtGui and QtCore headers, the compilation time will suffer greatly, and you should try to avoid that.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224