Important: Please read the Qt Code of Conduct -

[SOLVED] [File Organization] Creating an organized project with Qt Creator

  • Hello Qt devs!

    I'm developing a Qt GUI using Qt Creator. By default, when you create a new file (be it a header, a source file...), it puts in the current directory of your project.

    Is there a way to add new folder, say, I want to put all my dialogs inside a folder, inside Qt Creator so that the .pro updates itself, or should I do that outside Qt Creator and update the .pro myself?

    Also, this may not be the place to ask this, but do you have general tips on how to organize a project which has a lot of different classes?

    Thanks everyone!

  • Moderators

    Take a look at how Qt Creator does this:

    We use .pro and .pri files to group files into libraries and plugins (with their files in separate directories).

  • Thanks for the tip! Does Qt Creator recognizes this tree structure or does it list everything in the folders Headers, Sources and Forms ? Are the plugins in separate projects?

  • This is how I organize my bigger projects (using .pri files):

    I use pri files like this:
    SOURCES += source1.h
    HEADERS += header1.h
    and in the main project .pro file:
    It works perfectly in Qt Creator (if you do right click on the appropriate folder you can add files to that .pri like you would to a normal project).

  • Thanks, that's exactly what I was thinking of!

  • Just to add to that you also have the possibility to add multiple projects to a bigger projects using the qmake subdirs template. This basically just tells qmake to build all the projects you specify (and in the order you specify):
    TEMPLATE = subdirs
    CONFIG += ordered #do it in the order specified
    SUBDIRS = uis languages programmers

  • I'm not sure how actually do this in Qt Creator. Do you create a new Qt Creator plugin project and modify the .pri so that they get included in your project?

  • I usually just do New->Text File and change the extension to .pri!

  • Ok, now I understand. You write it manually initially, then Qt Creator takes cares of it.


  • Yeah, sorry I should have mentioned that. Maybe if have time, I'll code a wizard for that and do a merge request.

  • Yes, that would be incredibly helpful. However, I'm sure a little noob documentation in Qt Creator itself could be good. It may be my fault, but I didn't find anything in Qt Creator's docs about that.

    Anyway, thank you for your answers!

  • Sorry to play threadcromancer, but I want to organize my files like this. I can see how to create a new text file and rename it to .pri, but it just gets sorted under Other Files. Never worked with plugins before. Can someone please write a step-by-step on how to organize files like in the image posted above?

    EDIT: Hm...ok I'm guessing it's add library? Didn't think of that before. BTW, what does the following mean for the .pri file? Specifically $$PWD?


  • When you create add a text file and rename to .pri in Qt Creator, it will take some time before Qt Creator parses your new files and recognizes the new folder structure. When it does, your project will look like the picture above.

    And $$PWD dereferences the environment variable PWD, or print working directory, which is the directory you set for your project in Qt Creator, if I remember well.

  • So I got this stuff working. I uploaded the project to a git repository and cloned it to another computer. However, the project fails to recognize pretty much every header file, one at a time. For example it says it doesn't recognize one header file, but if I comment that out, then it doesn't recognize a different header file, and so on. Any ideas on why Qt won't recognize on one computer while it recognizes and compiles fine on the other? All the pri files and pro files are unchanged from the working computer.

  • The only thing I could see that would cause that would be using absolute paths. Can you show us your .pro file?

  • @#-------------------------------------------------

    Project created by QtCreator 2012-06-27T21:24:38


    QT += core gui

    TARGET = Singularity
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp \

    HEADERS += mainwindow.h


  • Ok, I'm lost.

    What do you comment out for a header file not to be recognized? And what do you mean, not recognized? It doesn't show up in the left-hand-side list? Since I use .pri files, I always get the hierarchy shown by loladiro.

  • By not recognized I mean a compiler error. The error that I get is:

    error: C1083: Cannot open include file: 'internal_lib/widgets/sheet_wp.h': No such file or directory

    I tried commenting out the include statement that includes sheet_wp.h, but I only got another of the same error but for a different header file. It's strange because Qt Creator recognizes all the files in the Project pane and all the files are in the right folders.

  • Wow. Fixed it but I don't completely understand why it worked. I looked up the definition of PWD because I didn't fully understand what it meant:

    "The PWD variable specifies the full path leading to the directory containing the current file being parsed. This can be useful to refer to files within the source tree when writing project files to support shadow builds."

    The word shadow builds caught my attention. I noticed that on the original project it was using a shadow build and the ones I've copied over weren't. I reloaded the pro file and this time selected shadow build option, now it compiles fine.

  • Having just worked through this issue in QT Creator I found that the (simple) thing I didn't notice before if you click on the funnel shaped icon in the project view and un-check the Simplify Tree option it makes it look like the "loladiro picture": .

    I also did not get this to work:

    SOURCES += source1.h
    HEADERS += header1.h

    So instead I did this:

    SOURCES += $$CURRENT_DIR/source1.cpp
    HEADERS += $$CURRENT_DIR/source1.h@

    This works for me BUT
    If someone has time to explain, I would be curious to know why why modifying the INCLUDEPATH and DEPENDPATH variables didn't work.

Log in to reply