106

I would like to divide my Qt project into several directories because it is growing pretty large. However, when I click on browse in QtCreator, there is no 'Add directory' and no such thing in 'Add new'. Can this be done somehow?

lpapp
  • 47,035
  • 38
  • 95
  • 127
gruszczy
  • 37,239
  • 27
  • 119
  • 167
  • What is wrong with creating a sub-directory with your file manager? – Ariya Hidayat Jul 24 '09 at 09:57
  • 2
    I can create dirs manually, but I don't see them then in project browser. – gruszczy Jul 24 '09 at 10:31
  • 5
    @Ariya You should have at least up voted this as this is a feature available in most IDEs and people *will be* asking about it. – Piotr Dobrogost Jul 24 '09 at 16:29
  • Ah, you mean you want to have the subdirectory appears like a tree format? – Ariya Hidayat Jul 24 '09 at 16:51
  • 54
    With all due respect but Qt-Creator sucks in usability (regarding user interface) As a developer if you see that someone has to ask how to add a subdirectory to a project in a public forum, then you know that there's a serious problem with that IDE. – Edenshaw May 25 '13 at 16:31
  • @Edenshaw This question is very old, but I believe it's fixed by now. It's quite easy to create subdirs now: you just add the file into a subdir. And if you create a file inside a subdir with file manager, the Qt Creator auto-detects it (it doesn't show empty subdirs in the project tree, though). – GabrielF Mar 29 '14 at 15:37
  • @GabrielF Thanks for the info! I'll give it a try. – Edenshaw Apr 01 '14 at 21:53
  • Here your answer https://stackoverflow.com/questions/24959959/how-to-create-a-folder-or-a-subdirectory-for-a-project-in-qtcreator/24959960#24959960 – Erowlin Jul 25 '14 at 16:20
  • 4
    @GabrielF quite easy...? it's fixed...? Why can't we just select "create directory" (or "add directory" to be in spirit of Creator) like in most IDEs. It's the basic functionality, which even most crappy editors have - you can create directory in notepad! There shouldn't be a "method" to do such simple, basic task - it should be obvious. – doc Sep 19 '14 at 20:04
  • @doc if you select "add file" and click on the search button for the file name, the standard OS file dialog will open, and you can create your directory from there. This is easy enough for me. – GabrielF Sep 24 '14 at 16:02
  • @Edenshaw, have you found the way to create, remove files/directories in Qt ? I am still unable to locate the way, I am coming from Eclipse (which is usually user friendly). – iammilind Oct 08 '14 at 07:33
  • @iammilind, Sorry can't help you with this. I stop working on the project involving QtCreator. – Edenshaw Oct 08 '14 at 16:03
  • Are these answers still valid in 2019 ? I tried them but got the "no rule to make target" error – Greg Mar 05 '19 at 14:40

9 Answers9

40

One method you could use is to add a project include file for each sub directory.

Qt Creator displays these in the GUI in a nested fashion, and allows you to add files to them.

e.g.

in project.pro

include(folder1/include.pri)

in folder1/include.pri

HEADERS += MyClass.h
SOURCES += MyClass.cpp

etc

Nick
  • 7,430
  • 2
  • 27
  • 35
  • Can I do this nesting from the IDE or do I have to manually set those files and dirs? – gruszczy Jul 24 '09 at 12:19
  • 1
    I had a quick look, and I think you have to add the include files manually, but when adding new files to a project Qt Creator will allow you to add them to an include file rather than the main project file. – Nick Jul 24 '09 at 12:30
  • 3
    OK, hopefully they will add this feature in new version. – gruszczy Jul 24 '09 at 13:12
  • 2
    Please consider my answer : https://stackoverflow.com/questions/24959959/how-to-create-a-folder-or-a-subdirectory-for-a-project-in-qtcreator – Erowlin Jul 25 '14 at 17:34
26

Answer : How to create a folder or a subdirectory for a project in QtCreator?

Prior to QT Creator 3.1.x, you can right-click on your project -> "add new..." and change the path to the folder you want.

Qt add new...

The folder must exists, Qt will not create it for you.

Add a new class and change the default folder Qt

Qt takes care of the path in your .pro file.

Qt takes care of the path in your .pro file

Qt takes care of the path in your .pro file

That's it !

Erowlin
  • 4,314
  • 3
  • 28
  • 58
20

Just had the same issue, and found out a relatively simple answer.

All you need to do to move file.cpp to newFolder is to rename the file (right click -> Rename) to newFolder\file.cpp.

Qt Creator will move it to the new folder and automatically update the .pro file.

Avidan Borisov
  • 2,975
  • 4
  • 21
  • 27
  • 3
    don't forget that the folder should already exist before the replacement – Anton Mar 18 '14 at 09:35
  • 2
    If the moved file is under GIT control, does Qt creator also take care of necessary changes in GIT? – Silicomancer Apr 17 '18 at 22:01
  • @Silicomancer Git is handling moved files for you, not Qt Creator. There is nothing you have to do but moving the file via this described work-around (using "rename") in Qt Creator. If you were moving the file just in the file system Git would recognize this but the path of the file would not be adjusted in the Qt Creator's `.PRO` file... – R Yoda Nov 04 '18 at 10:35
  • @Silicomancer Yes. If qtreacor recognized that the project is versioned with git it will duplicate all actions. This includes, at least, file creation, deletion, rename & move. – Equilibrius Oct 31 '19 at 17:37
9

Starting from version 1.2.90 Qt Creator shows subfolders which exist in project's folder as branches in project's tree if only Filter tree option is not set to Simplify tree.

Piotr Dobrogost
  • 38,049
  • 34
  • 218
  • 341
7

It only seems to be impossible to create sub-directories in QT-CREATOR.

Try the following:

  1. Create a number of sub-directories, with a file-explorer or by command line within the project-folder (for example net/, gui/, test/, data/ ...)!
  2. Move exisiting files into these new folders. And change their paths within the *.proj file!
  3. Create new also files from beginning within the new folders (By AddNew...)!

... QT-CREATOR displays only such folders which contain files that are written with their names into the *.pro or a *.pri file. At root level QT-CREATOR distinguishes between HEADERS, SOURCES, FORMS and OTHER FILES. Within these root folders you can find project-own subfolders, repeatedly. (Not covered in this text is splitting into sub-projects.)

Thiem Nguyen
  • 6,257
  • 7
  • 28
  • 50
CWIMMER
  • 151
  • 2
  • 4
  • True. If a Qt project is viewed inside Qt Creator and `pri`s projects are used properly the IDE displays things in a nice nested fashion. However on the filesystem level all files (except when using `subdir` and creating sub-projects) are in the same level of directory-hierarchy. This makes things rather cucumbersome when someone looks at the project in a file explorer (Windows Explorer, Thunar, Nautilus etc.). If proper file structure is require then doing things manually (as @CWIMMER suggests here) is the only way to go. – rbaleksandar Nov 02 '16 at 12:05
5

When you create a new Class in your Qt-Project, you can choose the path in this wizard and hereby specify new folders like DAL, BO, UI, ...

3

You can create a sub-directory as long as you have a file you wish to create in it. Go to the parent directory, and "Add" a file to it. "Browse" for the location and create a new folder inside the browse window. Agreed, that is not quite intuitive.

michaeljt
  • 978
  • 7
  • 17
0

Here's what I have done:

  1. In the Project Folder (outside the IDE), create Directories that you'd like to put your code in and move your source files into those directories.

    • Say you put "foo.cpp" and "foo.h" in the directory "foo".
  2. In your "*.pro" file, go to each line that references the source files you moved and add the directory name, followed by '/' in front of the source file name.

.pro before Step 2:

SOURCES += main.cpp \
foo.cpp

HEADERS  += \
foo.h \

.pro after Step 2:

SOURCES += main.cpp \ 
foo/foo.cpp

HEADERS += \
foo/foo.h
  1. Rebuild your project to test.
0

When my 'data' directory only had one sub-directory 'model' it just appeared as "data/model". After adding 'dao' as another sub-directory it displayed data with the traditional +/- manner to reveal model and dao.

James Wald
  • 13,196
  • 5
  • 48
  • 63