Qt what headers to include?

前端 未结 4 1445
我在风中等你
我在风中等你 2021-02-20 11:42

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

#include 
#include 

or



        
相关标签:
4条回答
  • 2021-02-20 11:56

    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.

    0 讨论(0)
  • 2021-02-20 12:02

    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.

    0 讨论(0)
  • 2021-02-20 12:06

    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.

    0 讨论(0)
  • 2021-02-20 12:10

    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.

    0 讨论(0)
提交回复
热议问题