Organize Qt project files in folders, and simplify the includes - qt

Brief
How do I tell my sub-project which are its subdirectories, in pro file, and not have to prefix the subdirectories in each #include directive ?
Problem description
I have a large project, containing multiple libraries. Also, I am looking at one library that has a large number of classes. I am trying to organize it in such a way that it is easier to see everything at a glance.
I am using Qt Creator. The only way to make the logical grouping I am thinking of, is to create folders.
The "pro" file looks logical:
SOURCES += sample.cpp \
class1.cpp \
class2.cpp \
interfaces/interface1.cpp \
interfaces/interface2.cpp
In each file that I must include one of the headers I must also add the folder:
#include "interfaces/interface1.cpp"
I would not give this a second thought, except...
For an outside library or application, I can add in their pro file (or include a pri file) to tell where the current library files are located:
INCLUDEPATH += $$PWD/../Sample
INCLUDEPATH += $$PWD/../Sample/interfaces
This creates the magic: from any outside library or application, I don't need to prefix the include directive with the folder location.
#include "interface1.cpp"
I have not been able to make this work from the current project. I don't understand why, I would like to know how to make it so that I no longer need to ad the folder path in the include directives (while still keeping the visual logical organization of my files).
Summary
For the structure above, adding
INCLUDEPATH += $$PWD/../Sample/interfaces
In the po file for an outside project allows me to include files like this:
#include "interface1.cpp"
I want to be able to do the same from the current project files. Right now, I am forced to include files like this:
#include "interfaces/interface1.cpp"

Create .pri file for subdirectory. And include .pri in .pro file.

Related

If you include a .pri, shouldn't that .pri's include path affect your #includes?

If I include(someOtherProject.pri) in my .pro, and that other project file (.pri) has INCLUDEPATH += . in it, then shouldn't my files in my .pro find those headers when I #include them? I cleaned and ran qmake on everything, regenerated the .pri file by simply copying the .pro from that other project and renaming it with the .pri extension, but when I #include a file from that other project, compiler says it cannot find it.
I wouldn't expect that I must also edit the INCLUDEPATH of my working .pro file if I am already using include(someOtherProject.pri) -- that would make the point of using include(someOtherProject.pri) unnecessary altogether.
The solution is to have INCLUDE PATH += $$PWD/ in the .pri instead, as this will expand out to the proper hard-coded path no matter where it is included.

Qt Error: cannot open include file: 'opencv2/core/core.hpp' no such file or directory

I want to build an opencv project using Qt 5.1_VS2010. I have included the path for opencv include files. But when I run the code, I get the error that specific .hpp file does not exist! I have checked in the directory and it actually exists there! another point that when I was trying to include it in my main c++ file, the auto completion guide actually helped me complete the whole path! so it means the directory and the header file was already recognized! anyway, part of my .pro file that includes the path is like this:
INCLUDEPATH += "C:/opencv/build/include"

Including a .pri file in multiple .pro project file with different relative paths

Hi I have some files in a dir called COMMON_SRC and a .pri file for these src files : Common.pri
and two projects in diffrent relative paths to common files.
I wanna use only one Common.pri file.
Can anyone help me?
Can I use some variable in each .pro file which tells the Common.pri file the relative path to project?
something like $$PWD ?
_PRO_FILE_PWD_ is what you want (docs).
If I understood your question correctly, assume You have a project tree like below:
/
├─common_src
├─test
└─lib
and you have common.pri in the common_src folder which points to source files. Imagine you like to include sources in test and lib projects.
So, change files path in common.pri in order to include current path as below:
SOURCES += \
$$PWD/document.cpp
HEADERS += \
$$PWD/document.h
now, include common sources in other projects as this:
lib.pro:
#...
include(../common_src/common.pri)
#...
This trick causes to included path be interpreted as relative to the current project path.

Adding each subdirectory's path to qt pro file variable

I want to add path of each subdir having .h file to a qt pro file variable
I found this http://doc.qt.digia.com/qt/qmake-project-files.html#built-in-functions-and-control-flow for use of a for loop but don know how to use if to do above task.
INCLUDEPATH +=$$(MITK_INCLUDE_PATH)
When I wrote this (where path variable having only outer dir's path) its allowing me to include a header in source as:
> #include"ITK-src/Code/BasicFilters/itkAbsImageFilter.h"
and qmake is running properly but when I am building this project its asking for another .h on which above .h depend. Before this I have worked with adding 3'rd party libs in which I gave path of each header file's location to INCLUDEPATH as here but if there is any other way I want to know possibility.

The right way to use a custom library in Qt?

I have a custom library to be included in Qt. Currently, it is included by adding the followings in .pro file.
INCLUDEPATH += ...
DEPENDPATH += ...
LIB += ...
Since the library is used in most of my projects. I am wondering that if there is some way to make my custom library to be INTEGRATED with the QtSDK? Maybe it can be included by syntax like the build-in components
QT += my_custom_library
and everything (header file include path, lib file include, etc...) will be done.
Is the integration possible?
You are probably looking for to create a mkspec file which then used with CONFIG option. So create a file to $QT_HOME/mkspec/features/mycoollib.prf that contains your instructions. There should be plenty of examples in that directory. The name of the file declares it's usage, so the file mycoollib.prf would be used like:
CONFIG += mycoollib

Resources