Important: Please read the Qt Code of Conduct -

#include with the .h or without

  • Hi,

    I see in general the Qt headers are included using #include <QtHeader> instead of #include <QtHeader.h>.
    Are there any reasons for using the one above the other?

  • I believe there is doesn't matter on how you specified name in angle brackets. Following by C++ notation it maters only if you specified header name in angle brackets or in quotes (it's changes order of source file searching). Actually you should use angle brackets with header name, than filename in quotes. It's the main difference.

    Answer for your particular question is there:

    user@laptop:/usr/include/qt4/QtCore$ cat QMimeData
    #include "qmimedata.h"
    user@laptop:/usr/include/qt4/QtCore$ head qmimedata.h
    ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
    ** Contact:
    ** This file is part of the QtCore module of the Qt Toolkit.
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in

    <QtHeader> it's file too, with some set of #include directives, while <QtHeader.h> could be file exacty related to your question like "qtheader.h" without any additional includes.


  • @TheBadger I believe both forms #include <QObject> and #include <qobject.h>are okay. Note the difference in case. This matters on some platforms.

  • Good one link on Stackoverflow

  • Lifetime Qt Champion


    Using the h-less header set is more complete in terms of class names. You can have several classes coming from the same .h file e.g. QVBoxLayout, QHBoxLayout both come from qboxlayout.h however using #include <QVBoxLayout> makes you intention clearer in your code. Another advantage of doing so is that if a class was to be moved to another header for whatever reason, then your application would still be able to build.

    Hope it helps

  • Moderators

    From the C++ standpoint it's purely a matter of paths to be searched.
    But a lot more important here is that the .h headers are considered Qt internal and are subject to change without notice (no Qt documentation mentions them if I'm not mistaken), while the .h-less ones are considered publicly maintained API of Qt.

  • One benefit of using Qt header files without extensions is that it separates your custom header files from Qt ones in some way. It makes it more clear and clean that which header is for Qt and which one is yours.

Log in to reply