Qt qmake enhancement – how to generate structure VS project

The problem

One of big disadvantage when creating Visual studio project from .pro files using qmake is missing support for hierachical folders. All files are stored in in four folders (filters) named “Source Files”, “Header Files”, “Generated Files” and “Form Files”. When you have large project, this arrangement is really hard to use.

Simple Solution

qmake has undocumented setting parameter “flat”. Using this switch, you can tell qmake to create filters in Visual studio dependent to directory structures of files. Only drawback of this solution is continuing sorting to “Source files”,”Header files”,…

So result of this solution will look like this:

To achieve this behaviour, only thing what have to be done is add following line to your .pro file:

CONFIG -= flat

Final solution

But what to do when you want to have all files stored together in structure dependent on directory structure? Only solution which I found is update qmake project for myself. This update is simply and here is what is need to be done:

Add new method initHeaderAndSourceFiles to file msvc_vcproj.cpp and .h with following content

void VcprojGenerator::initHeaderAndSourceFiles()
{
	vcProject.SourceFiles.Name = "Source And Header Files";
	vcProject.SourceFiles.Filter = "cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx;h;hpp;hxx;hm;inl;inc;xsd";
	vcProject.SourceFiles.Guid = _GUIDSourceFiles;

	vcProject.SourceFiles.addFiles(project->values("HEADERS"));
	vcProject.SourceFiles.addFiles(project->values("SOURCES"));
	if (usePCH) // Generated PCH cpp file
		vcProject.HeaderFiles.addFile(precompH);

	vcProject.SourceFiles.Project = this;
	vcProject.SourceFiles.Config = &(vcProject.Configuration);
	vcProject.SourceFiles.CustomBuild = none;
}

and update method VcprojGenerator::initProject() in msvc_vcproj.cpp file. Replace following two lines (currently located on line 762)

initSourceFiles();
initHeaderFiles();

with following code:

if ( project->isActiveConfig("grouped") == false ) {
	initSourceFiles();
	initHeaderFiles();
}
else {
	initHeaderAndSourceFiles();
}

Now you can add new flag “grouped” to your .pro file. After that, source and header files will be merged in the visual studio project tree.

CONFIG -= flat
CONFIG += grouped

And here is result screenshot: