Important: Please read the Qt Code of Conduct -

[SOLVED]having trouble including a file

  • hello,

    I am a beginner in qt programming, and stumbled across the following problem.

    I have this project in which i needed to include some files from other projects however when I tried including a certain file the compiler flagged it as an error. This file belonged to a previous project, and was added to the current project by right clicking on the project tree and clicking on "add existing files". additionally, when I added another file from a different project (both located in the same directory though), it was able to be included on the project just fine. Additionally, when I checked to see if the file (the one that is having trouble being included), is not in my current project directory. does anyone know what may be wrong? perhaps the compiler does not know where to look for the file (even though it found the other one) . all these files are header files.

    any help is greatly appreciated

  • Moderators

    the compiler flagged it as an error

    What compiler?
    Which IDE?
    What did the error say?
    What are the paths to your project and the include file in question?
    What did you try already?

  • compiler: MSVC 2013 64bit

    ide: qt creator 2015

    error: no such file or directory found

    path : C:\Users\MyName\Documents\qtDesighn

    tried searching for solutions online, and putting folder in different paths

  • Moderators

    Does the error also say the path to the file it can't find?

    Can you paste the full path (including file name) of the include file, the path to where your .pro file is located and content of the HEADERS += section in your .pro file?

    Btw. Qt Creator is not versioned with years as new versions come out a couple of times in any given year, so there's no Qt Creator 2015. Current version is 3.5.0 (see Help->About).

  • sorry, thanks for the reply

    these are the contents of my HEADERS += file

        HEADER +=  mainwindow.h \
    finddialog.h \
    sortdialog.h \
    spreadsheet.h \
    ../../hideDialog/sortdialog.h \
    ../../qtdesighn/cells.h \

    it is cells.h that is having trouble being included

  • the full path of cells.h is file:///C:/Users/myName/Documents/qtDesighn/cells.h

  • Moderators

    The paths here are relative to the location of the .pro file. As it is now it is 2 directories up and then qtdesighn directory.
    This is correct if your .pro file is located in C:/Users/myName/Documents/<something>/<something>/
    If that is not the case then you need to adjust the path in the HEADERS section.

    Oh, an for the record. This makes this file part of your project. If you only want to include it without making it part of the project you should remove it from HEADERS variable and instead specify a path to directory it is placed in in the INCLUDEPATH variable.

  • I want the file to be part of the project, so i guess i will try to change the file path in the HEADERS section of the .pro file

  • sorry, but i tried changing the HEADER section of the .pro file as such

        HEADERS  += mainwindow.h \
    finddialog.h \
    sortdialog.h \
    spreadsheet.h \
    gotocelldialog.h \

    C++/mainWindow/cells.h is the exact same directory that the .pro file is located in.

    however cells.h is still not being included. and also, cells.h is not showing up in the directory of the project even after i added it to the project.

  • Moderators

    That makes no sense. You said the cells.h file was located in C:/Users/myName/Documents/qtDesighn. If so then of course adding ../../C++/mainWindow/cells.h to the HEADERS won't work as there is no such file in that directory.

    As I said the paths in HEADERS are relative to the location of the .pro file. .. means "1 directory up".
    You didn't post the path to the .pro file, so I'm gonna make up a couple of examples so that you can see how that works.

    HEADERS += some_header.h

    HEADERS += include/some_header.h

    HEADERS += ../some_library/include/some_header.h

    HEADERS += ../../some_library/include/some_header.h

    I hope you see the pattern. If you post a full path to the .pro file I can tell you what you should put in the HEADERS.

  • alright, i made a new project called tests in which my .pro file has the current path:


    the file i am trying to include is:


    in my HEADERS section of the .pro file i included sortDialog.h as so:

       HEADERS += ../hideDialog/sortdialog.h

    however when i try to include sortdialog.h in main as so:

         #include "sortdialog.h"

    the compiler flags it as an error saying "no such file or directory found"

  • Moderators

    HEADERS += ../hideDialog/sortdialog.h

    I'm assuming this correctly adds this file to the project and it shows up in the project tree in Creator, right? That's one thing, but...

    #include "sortdialog.h"

    Adding a file to project doesn't mean you're automatically adding its directory to the paths that are searched for includes.
    The above would make the compiler look for the include in the same directory as the file including, which is not the case here.

    You can do one of these:

    • specify the correct path in the include statement: #include "../hideDialog/sortdialog.h"
    • add the directory to searched paths in your .pro file: INCLUDEPATH += ../hideDialog and then you can use #include "sortdialog.h"

  • so, that was the problem. my bad, and thank you very much for the replies, thanks

Log in to reply