In a large Qt project in which a lot of Qt and project headers are included in every file, it is easy to:
- include extra Qt files that don't need to be included because they are already included in another Qt file (for example,
qbytearray.h
is included inqstring.h
). - forget to include needed Qt files because they are already included in other included project files (for example, the compiler finds
qstring.h
included in another of your files and doesn't complain). - left included extra Qt files that are not needed anymore after a modification.
I have been also reading that, even with modern compilers, it is better to include the files needed, and only those, instead of the easy way of including more generic headers like QtCore
and QtGui
.
The rule seems easy: include only everything you need and don't depend on other included files in case they change in the future (for example, qstring.h
could not use qbytearray.h
anymore, which is also true for project files), but it's not so easy to achieve. And Qt Creator doesn't help much with that, because when you begin to write QStr...
it auto-completes with QString
and compiles, and you don't even wonder why nor think of including the header.
Is there a list of Qt headers dependencies or an automatic Qt tool or a rule or something to make sure I have chosen all the headers I need and nothing else? The question is general to C/C++, a way to get the optimum header dependency.