Qt World Summit: Submit your Presentation

[SOLVED] Problem with source files in multiple directories

  • I'm currently writing my Qt app and am storing the interface source files in a subdirectory of the root directory called 'ui', so that @main.cpp@ which is kept in the root directory, uses the line @#include ui/mainwindow.h@ to import the main window. I've already compiled and ran this app with the files all sitting in the one directory and there were no problems then, but now after I've split them up qmake tells me it can't find them when I try to make the @.pro@ project file. Is there anything special that I'm supposed to do to get qmake to recognise source files in child directories?

  • Did you use the ui subdirectory in you pro file?
    Eg. :
    Headers += ..../ui/mainwindow.h

    Also do the same with other files like sources

  • Wouldn't that be wiped out each time I build the project? Isn't there a way have qmake pick it up the same way it found the headers the first time round?

  • @INCLUDEPATH += ui@ maybe?

  • No @qmake@ looks in your pro file

    Once you have a pro file you don't want it to be generated automatically. Suppose you need a library in another path and you added it manually...

    I suppose you used @qmake -project@ the first time? Like this you let qmake autogenerate its contents.

  • Thanks a lot for your help. That's done the trick. I didn't realise you only generated the @.pro@ file once.

  • So...I'm working on a project that's getting bigger than I originally thought, and I'd like to retroactively introduce a directory structure to my source and header file organization. I notice that Creator doesn't have an "add directory" option.

    Should I just add the directories in the file system, start moving files around, and run qmake again? Any "gotchas" that I should be aware of?


  • @mzimmers: it is true, Creator has nothing to do with file management. I think that the right perspective you should see it is as a manager for the project with all the related stuff.
    The file management should be left to the file manager / file system tools and applications. When you need to split or add new folders you will do it externally. Then instead searching something like adding new files etc. there is the command "add existing file" As you use it you can add almost anything you need in your project from inside the project folder or outside it.

    I think that the best practice (maybe ???) procedure will be :

    Add the files in the folders where you need

    Go in the project tree from the Creator and right click with the mouse selecting add existing ifle

    Explore your folder tree and add the files you need from the desired folders (also outside the project folder tree)

    If these are resouces (i.e. images or QML documents) and you are using environments like Harmattan you should add these files in the project resource file too.

    If these files need a .pro file update edit the project file as suggested by Eddy above.

    Two notes:

    • If these operations should be done with a project managed too with a git or svn versioning system when you remove files from a project or move them remember to do the same with the repository to avoid problems in the further commits: not all the repository operations are managed by Creator.
    • In versions previous Qt 4.7.3 the command add existing files can be found right-clicking only the root of the project in the project explorer. Starting from the last versions of Qt-Creator included in Qt 4.73 this command appears in the contextual menu in all the folders.

    Hope it can help.

  • Thanks, Enrico. I just experimented with one pair of files, and it seems to work OK.

    By the way, your last point seems to be in error: I can perform the "add existing files" command by right-clicking on directories under the root. But, this is a minor point.

    Thanks again.

  • Happy to hear that is works.
    Rgarding the question that the command work anywhere, it is possible that I don't remember in what case it doesn't work. But it doesn't matter, it was just a reminder that the command exist also if in some conditions it does not appear.

    Note that regadless where the file are they are put in sources, headers, other files etc. depending by the kind of file your are adding to the project.


Log in to reply