Build targets with different templates in Qt - c++

Hi I am relatively new to Qt and I could not find a viable solution to this so far. I have the following project structure
ALPHA-app
alpha.pro
ALPHA-Desktop
alpha-desktop.pro
ALPHA-Shared
alpha-shared.pro
ALPHA-Common
alpha-common.pro
ALPHA-Tests
alpha-tests.pro
I basically have to run alpha.pro which builds all the projects and creates an executable
alpha.pro,
TEMPLATE = subdirs
DESTDIR = .
SUBDIRS += \
ALPHA-Common \
ALPHA-Shared \
ALPHA-Desktop \
ALPHA-Tests
ALPHA-Desktop.depends = ALPHA-Common
ALPHA-Shared.depends = ALPHA-Common
ALPHA-Tests.depends = ALPHA-Common
ALPHA-Tests.depends = ALPHA-Desktop
the main executable that will be run is present in the ALPHA-Desktop project (given below)
TARGET = ALPHA #this is in ALPHA-Deskop.pro
TEMPLATE = app
Now I created ALPHA-Tests to make a project to run unit-tests. It depends on ALPHA-Desktop (Hence I wrote the .depends statement in the ALPHA project)
To run the ALPHA-Tests, I need to create a static library of the ALPHA-Desktop folder. Hence I wrote the following lines in ALPHA-Desktop.pro file
TARGET = ALPHA-Desktop #section 1
TEMPLATE = lib #section 1
CONFIG += staticlib #section 1
TARGET = ALPHA #section 2
TEMPLATE = app #section 2
When I run the ALPHA.pro file, the ALPHA-Desktop library is not getting created. Only the ALPHA executable is being created. If I interchange the positions (i.e., put section 2 above section 1) then only the libALPHA-Desktop.a file is created and the executable is not created.
How do I solve this? (I am using qmake version 3.0, Qt version 5.4.2 on ubuntu 15.10)

TEMPLATE, TARGET etc are always global for one .pro file. While this is never explicitly spelled out in the qmake documentation, it is obvious once you realize that the qmake syntax aims to be declarative. The last line setting a variable will always prevail (except if you overwrite the variable via the command line).
If you want to create a static library and an executable, you need to have two .pro files (probably again integrated with a subdirs .pro file).
Note though that this doesn't necessary mean that you need different subdirectories, since multiple .pro files can also exist in the same directory:
ALPHA-Desktop.pro:
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += ALPHA-Desktop-staticlib.pro ALPHA-Desktop-app.pro
ALPHA-Desktop-staticlib.pro:
TEMPLATE = lib
CONFIG += static
# ...
ALPHA-Desktop-app.pro:
TEMPLATE = app
# ...

Related

UI_project.h not found for QT project

I made a QT GUI project in VS 2019. It ran perfectly through VS. Copied the entire project files directory to Linux partition. Ran qmake -project in the directory containing the 'test2.sln' file to create a 'test2.pro' file. Opened the '*.pro' file through QT Creator. It imported everything fine. But, when building it it errors out saying "ui_test2.h file not found".
The test2.pro contains:
TEMPLATE = app
TARGET = test2
INCLUDEPATH += .
DEFINES += QT_DEPRECATED_WARNINGS
HEADERS += test2/test2.h test2/x64/Release/uic/ui_test2.h
FORMS += test2/test2.ui
SOURCES += test2/main.cpp test2/test2.cpp test2/x64/Release/rcc/qrc_test2.cpp
RESOURCES += test2/test2.qrc
What should I do to fix this?
1) remove folder with build (rm -r nameOfFolder)
2) rename your UI file (test2.ui -> mainwindow.ui) for example
3) check out name of own class and name of qt class in UI file (you can open it via vim or nano)
After that try to rebuild your project!
I hope you'll have done this with good results!

Standalone Application Qt

I have a Qt application that needs a specific folder and some files. This folder is now located inside the project folder. Is there any way to make this folder be "generated" when I use the make command to create the Standalone version of the program.
I was thinking about changing something in my .pro file but could not figure out any solution that way.
The folder contains some .lua code that I need to make the software work properly if the user select some functionalities.
One solution that copy these resources to destination folder the binary installed via qmake. Please refer to the link enter link description here
Some answers are out-of-date but inspire enough.
Possible solution(see http://doc.qt.io/qt-5/qmake-advanced-usage.html):
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
SOURCES += main.cpp
lua_dir.target = .makedir
lua_dir.commands = test -d lua_dir || mkdir lua_dir
QMAKE_EXTRA_TARGETS += lua_dir
PRE_TARGETDEPS += .makedir
You should replace lua_dir.commands = test -d lua_dir || mkdir lua_dir with appropriate to you command, I use bash command intepreter here.
you can change DESTDIR qmake variable to compile binary file straight to specified directory.
you can use INSTALLS:
someTarget.path = $$OUT_PWD/MyFolder
someTarget.files = $$PWD/SomeDirToCopy/*
INSTALLS += someTarget
this will copy all files from ProjectDirectory/SomeDirToCopy folder to BuildDirecory/MyFolder after you run make install in build directory.

QT multiple project configuration

New to QT but can't see how I can't switch between debug and release configurations across multiple projects.
I have a session with 20+ projects in it, and switching between 4 configurations means going into each one and selecting a different config, which is just a different flag passed to the makefile. This is far too time consuming, is there a quicker way?
You can make a Subdirs project and add your subprojects to its .pro file :
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
project1 \
project2 \
project3 \
...
Here the project directories should be added in the Subdirs project directory alongside its .pro file. Notice that the name of the .pro file of the subprojects should be the same as it's folder name. This way the subprojects are detected and listed in the Projects pane automatically.
You can create a new project like foobar.pro:
TARGET = foobar
TEMPLATE = subdirs
SUBDIRS += ../foo \
../bar
This will enable you to compile foo.pro and bar.pro on release/debug mode with one command.

qt build output directory being totally ignored

i've been using qt recently and was organizing some projects into a Subdirs to begin a larger project, and went to manipulate the pro files so as to place my build files where i want them. However, the changes i make to any of them are being ignored. I have a main project that compiles to an executable, and then two other projects that compile to DLL's. I would like for the executables and libraries to end up in the same directory, where the subdirs pro file is located. Here is the pro file for my subdirs that i've tried:
TEMPLATE = subdirs
SUBDIRS += \
MainWindow \
FileLoader
Debug:DESTDIR = $$_PRO_FILE_/debug
Debug:OBJECTS_DIR = $${DESTDIR}/.obj
Debug:MOC_DIR = $${DESTDIR}/.moc
Debug:RCC_DIR = $${DESTDIR}/.rcc
Debug:UI_DIR = $${DESTDIR}/.ui
Release:DESTDIR = $$_PRO_FILE_/release
Release:OBJECTS_DIR = $${DESTDIR}/.obj
Release:MOC_DIR = $${DESTDIR}/.moc
Release:RCC_DIR = $${DESTDIR}/.rcc
Release:UI_DIR = $${DESTDIR}/.ui
After that, i put that same path block in all three of the subprojects pro files, and still all the build files end up in a directory in my Qt Projects directory called "build-Chromatic-Desktop_Qt_5_1_1_MSVC2010_32bit-Debug" (Chromatic is the name of the project). Can someone help me wrangle my pro files to do make this stuff where i want it? Again, i want all the build files for all projects in the directory where the subdirs pro file is.
Instead of using _PRO_FILE_ you should use _PRO_FILE_PWD_ because the first variable contains the path to the project file in use and the second contains the path to the directory containing the project file in use.
In Qt Creator you should deselect the Shadow build checkbox: "By default, Qt Creator builds projects in a separate directory from the source directory, as shadow builds. This keeps the files generated for each build and run kit separate. If you only build and run with a single kit, you can deselect the Shadow build checkbox."

Installing files using qmake: how to get the executable too?

I'm building my app with qmake.
my project.pro file looks something like:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += [all source files]
HEADERS += [all headers]
After a build, I want "make install" to copy everything required in a subfolder, so I added the following:
package.path = $${OUT_PWD}/package
package.files += myapp.exe myapp.ini [other dlls]
package.CONFIG = no_check_exist
INSTALLS += package
I cannot have in any way my compiled binary to be copied alongside; I prefixed it with $$OBJECTS_DIR/ and others, but I can't find the right variable containing the path to the build directory!
It seems these variables are meant as a way to change qmake's behaviour, for example to change the build directory. I don't want to change it; I want to access it!
Is there any other variable I could use? Basically, I want to put in "package.files" the full path to the compiled executable binary.
Thanks!
qmake puts the complied executable (or library) in the location pointed to by DESTDIR.
Usually, when I want to put the binary and support files, I will set DESTDIR to the location I want to install stuff, then set any INSTALLS.path to DESTDIR.
DESTDIR = $${OUT_PWD}/package
package.path = $${DESTDIR}
package.files += myapp.exe myapp.ini [other dlls]
package.CONFIG = no_check_exist
INSTALLS += package