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?
开发者_如何学GoThanks!
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.
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.
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.
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.
精彩评论