Important: Please read the Qt Code of Conduct -

Basic question: Including headers with INCLUDEPATH doesn't work

  • Qt Champions 2019

    @Binary91 Where did I add the header file manually?
    I commented out the old line, please take a look again:

    QT       += core network
    QT       -= gui
    TARGET = test
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    #INCLUDES += lib/headers/myQLineEdit.h <-- this line is commented out
    INCLUDEPATH += lib/headers
    SOURCES += main.cpp

  • but where is the myQLineEdit.cpp ?
    You just added the main.cpp. The error occures in myQLineEdit.cpp, because it holds methods of myQLineEdit class in myQLineEdit.h that has a Q_OBJECT within...

  • Qt Champions 2019

    @Binary91 Your project is really broken:

    QT       += core network
    QT       += gui widgets
    TARGET = test
    CONFIG   += console
    CONFIG   -= app_bundle
    TEMPLATE = app
    HEADERS += lib/headers/myQLineEdit.h
    INCLUDEPATH += lib/headers
    SOURCES += main.cpp \


    #include <myQLineEdit.h> // absolute path or absolute path with whitespaces also doesn't work
    // ----- constructors -----
    myQLineEdit::myQLineEdit(QWidget* widgetParent) : QLineEdit(widgetParent)
       // stuff
    #include "../build-test-Desktop-Debug/moc_myQLineEdit.cpp"

    Without HEADERS moc_ file is not generated and the header file is not shown in QtCreator, so you should use HEADERS.

  • Qt Champions 2019

    @jsulm said in Basic question: Including headers with INCLUDEPATH doesn't work:

    #include "../build-test-Desktop-Debug/moc_myQLineEdit.cpp"

    you can change it to:

    #include "moc_myQLineEdit.cpp"

    It builds now on my machine.

  • Qt Champions 2019

    @Buckwheat You need to include moc_ files for your own classes which are derived from QObject (directly or indirectly).

  • Hi jsulm,

    so you say, I need to include the headers with HEADERS in the project file. Well, then I'm asking myself what the INCLUDEPATH command is for?

  • Lifetime Qt Champion

    @Binary91 said in Basic question: Including headers with INCLUDEPATH doesn't work:


    Its used to specify where to look for headers.
    So when it sees #include "YYYY" it will search for them there.

    as shown with

    INCLUDEPATH += ../../Common/CONET
    ../../Common/XML/pugixml-1.5/src/ \

    the pugixml.h is in the src and i can just use
    it like
    #include "pugixml.h"

    Without it I would have to do use full path to it.

    That is its role and it works fine here.

    You seem to expect that point to a folder with INCLUDEPATH,
    implies that all should be used ?

    Like all where already added to HEADERS ?

    There is one issue if it did that.
    For all HEADERS , it need to run moc on them so
    if you use INCLUDEPATH to point to a place with many .H files
    you be wasting tons of time running moc on those completely unrelated files.
    Hence HEADERS tells us what files to run moc on.
    Of course this could be fixed checking against which are actually seen via #include
    but it's not the way it works currently. :)

  • thank you mrjj, that was the explanation I was looking for!

    I didn't know that with HEADERS I tell the compiler which moc files should be generated!

    So all in all, I have to include those headers to my project and everything is going well.

    Thank you for your help.

    Kind regards,

  • @jsulm Actually you don't. When the build system sees the Q_OBJECT, it will build and compile a MOC file for you. It will link into the application and be part of it. No inclusion is necessary. None at all. The only think you would ever include is a form generated header file (ie. ui_MainWindow.h) that will hold the form variables for you.

    If doubting, watch your build on any system. You will see the CPP files compile and then the moc_*.cpp files compile and then the linker will have both on it. The MOC files tie your class to the signal/slots and meta object data all under the scenes. It is quite beautifully designed.

  • Lifetime Qt Champion

    If your QObject based class is in a .cpp file then you need to include the moc file at the bottom. See the unit tests form Qt for example.

Log in to reply