How to specify auto-generated resource files to Qmake? - qt

I have a Qt project with a german translation, Translation_de.ts, which is automatically compiled into Translation_de.qm via Qmake:
TRANSLATIONS += Translation_de.ts
...
QMAKE_EXTRA_COMPILERS += lrelease
lrelease.input = TRANSLATIONS
lrelease.output = ${QMAKE_FILE_BASE}.qm
lrelease.commands = $$[QT_INSTALL_BINS]/lrelease ${QMAKE_FILE_IN} -qm ${QMAKE_FILE_BASE}.qm
lrelease.CONFIG += no_link target_predeps
The generated Translation_de.qm is then compiled into the final application as a resource:
RESOURCES += Resources.qrc
where Resources.qrc looks like this:
<RCC>
<qresource>
...
<file>Translation_de.qm</file>
</qresource>
</RCC>
All of this works fine, except that the very first call to Qmake on a fresh checkout throws the following annoying warning:
RCC: Error in 'Resources.qrc': Cannot find file 'Translation_de.qm'
What am I doing wrong here? How do I correctly specify an auto-generated resource file like Translation_de.qm?

http://doc.qt.io/qt-5/qmake-variable-reference.html
CONFIG+=lrelease #generates *.qm files from TRANSLATIONS= to the directory builddir/.qm/
CONFIG+=embed_translations #adds them as qrc resources
so (other than)
CONFIG+=lrelease embed_translations
no qmake magic is required. Your qm files will be under :/i18n/ unless you specify otherwise with
QM_FILES_RESOURCE_PREFIX=/my/customtranslationdirectory

Create the generated files in qmake phase with e.g. system(lrelease...). Leave the other rules in place too so you don't have to rerun qmake when the input files are changed.

I think that what you want is just "ignore_no_exist" for lrelease.CONFIG
As far as I know the target_predeps ensures that it is executed before the 'normal' compilation steps are issued. So if it's really only about getting rid of the warning, just add the above given flag. Your qm creation should work once you execute the makefile that was created by the qmake call.
If your qm files are not created try to add:
PRE_TARGETDEPS += compiler_lrelease_make_all
Check out this link for more options that might help you.

Related

QT QML resource files do not recompile after changes

I'm using QT 5.9.1 working on Mac OS. My project is mobile App with C++ logic and QML UI Layer. All QML files are included into qml.qrc file, so in my .pro file I have
RESOURCES += qml.qrc
Inside qml.qrc there is a list of all resource files I use in Project, such as pictures, icons and QML files, in QT Creator it's displayed OK:
As you can see some QML files are located in ROOT path of qml.qrc when other files are in subfolders , e.g. "qrc:/Elements/".
So problem is that whenether I make changes in Files that located in root of qml.qrc - they are normally recompiled when I press build, rebuild, or clean and build, so I can see my changes. As a result in my build directory I see that qml_qrc.cpp (as I understand this file contains cpp representation of my resource files and is used to compile them) file is refreshed,my changes are applied and everything is OK.
Here is just piece of this file, which begins with cpp hex representation of MainPage.qml resource file.
/****************************************************************************
** Resource object code
**
** Created by: The Resource Compiler for Qt version 5.9.1
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
static const unsigned char qt_resource_data[] = { //
/Users/admin/QtProjects/LazuritApp-temp/MainPage.qml
0x0,0x0,0x7,0xd1, 0x0,
0x0,0x1e,0xbb,0x78,0x9c,0xd5,0x19,0x5d,0x6f,0xdc,0x36,0xf2,0x7d,0x7f,0x5,0x6f,
0xb,0x14,0xbb,0x69,0xa2,0xf5,0xae,0xed,0xa4,0xde,0xa0,0x77,0xf0,0xba,0x49,0x63,
0x20,0x45,0xda,0xda,0x68,0x1e,0xe,0x45,0xc1,0x95,0xa8,0x15,0x2f,0x5a,0x51,0xa5,
0x24,0xdb,0x5b,0xc3,0x40,0xda,0x2,0xed,0x1,0x79,0x28,0x70,0x68,0x71,0xcf,0xf7,
0xf,0xdc,0xbb,0x6b,0x1b,0x34,0x4d,0xfa,0x17,0xb4,0xff,0xa8,0x43,0x52,0x5f,0x94,
..............
But if I change those resource files located in subfolders, qml_qrc.cpp file is not refreshed, so my resource files are not rebuild, even if I try Clean, then rebuild. Even if I do "run qmake" manually and then rebuild. The only thing helps in this situation - to manually delete build folder (or to be more precisely you can just delete qml_qrc.cpp file). So then pressing "Build" will create new qml_qrc.cpp file, which will contain correct code, with changes I've done in my resource files.
Can someone help me or explain why this happens and what can I do in this situation? deleting manually and rebuilding is annoying, but placing all resource files in root path of project is also not a good decision...
I also tried to paste
update qml
qml_scenes.depends = $$PWD/QML Files/OrdersPage.qml
qml_scenes.commands =
QMAKE_EXTRA_TARGETS += qml_scenes
as was described here, but it didn't help
ok, after hours of digging in QTBUGS traces, stackoverflow and others forums I found solution, which somehow satisfies me..
1) Create script file (for me it's .sh file as I working on MAS OS, for Windows it will be .bat file) with "touch" command to qml.qrc file. In my case it contains 2 lines :
#!/bin/sh
Touch qml.qrc
2) Add Custom build step (Projects->Build Settings->Build Steps->Add Custom Process Step). Choose your created .sh file, choose working directory when you build is located. Make this custom step to be the first executed (before qmake and Make)
3)So, now changes in qml resource files will be compiled every time you build the project. Script will firstly touch our qml.qrc file, which will refresh it's modified date, so that qml.qrc (hence, our qml resources too) will be added to makefile dependencies.
it seems to be pretty rough way to solve the problem, but at least you don't have to Clean, Rebuild and so on..
If someone have better solution, please let me know)
Unfortunately, the solution given in a link provided in the comments seems to be incomplete and/or broken. But it can be done like this:
update_qml.target = qml.qrc
update_qml.commands = echo>>$${update_qml.target} # same as touch
update_qml.depends = $$files(path/to/resource/files/*, true) # recurse into subdirs
QMAKE_EXTRA_TARGETS += update_qml
PRE_TARGETDEPS += $${update_qml.target}
For those who interested in how it works, QMAKE_EXTRA_TARGETS produces the following Makefile rule:
qml.qrc: file1 file2 ...
echo>>qml.qrc
So issuing a command make qml.qrc will update the timestamp of qml.qrc as needed. However, to save us from manual typing it every time, we also make use of PRE_TARGETDEPS which fixes the main building rule:
$(DESTDIR_TARGET): qml.qrc $(OBJECTS) and other stuff ...
$(LINKER) flags objects and other stuff ...
So now make utility will scan the previous rule every time while building the target.

Can you specify the path to exe in qmake?

Let's talk about it in linux terms. I have a ".so" file. I want to make the executable dependent on it, look for it in the same directory. How do you do it through qmake? Or can this only be achieved through the use of QLibrary?
For example, when you have a ".so" file and want to use it in your project, in qmake you write:
LIBS += -L"path to the folder that contains your .so" -lSoName
But the path is hardcoded, as you can see, and I'm wondering what to write there to make the executable look for the ".so" in the same directory.
You use RPATH
You can configure your binary or library to find shared library (or dll) in current directory using RPATH directive that is emedded in the binary itself which the loader respects at runtime
1- Add the following in your .pro file
unix {
message("Adding RPATH to the app")
QMAKE_LFLAGS += -Wl,-rpath=\'\$$ORIGIN/\'
QMAKE_RPATH =
}
This will set RPATH of executable to current directory and your executable will first try to look for that .so in your current directory and then in standard directory (this process is explained here)
2- After you compile and create binary VERIFY that RPATH is set correctly
objdump -x <path/to/binary> |grep RPATH
it should say $ORIGIN
Compile time configuration:
CXXFLAGS += -L"/path/to/libmarylin.so/file" -lmarylin
There are a few ways to do it.
If the executable is going in the current directory, just do LIBS += -L$$(PWD) -lSoName.
If you're putting the executable into some other sub-directory, specified by some qmake variable like DESTDIR, use LIBS += -L$$(DESTDIR) (or whatever variable holds that directory).
Alternatively, you can add the directory with the executable to the runtime path of the executable, which gives the dynamic linker a list of directories to search for any unresolved libraries at runtime. This can be done with QMAKE_RPATHDIR += -L$$(PWD) and LIBS += -lSoName, which will tell the linker to look for unresolved libraries in the current directory where qmake is run.
Some operating systems may also include the current directory in the runtime search path for libraries by default, in which case just doing LIBS += -lSoName should be sufficient. That is platform-dependent, though, while the above solutions are not.

Is it possible to add a dependency to your Makefile in qmake?

Currently, I have a file which contains some version tagging information which is used by the .pro file and parsed by qmake, but the problem is that when that file changes qmake is not re-run.
Is it possible to add something to the .pro file so that qmake will treat this file as a dependency for the Makefile?
Sometheing like this should work:
depend_on_file.target = depend_on_file
depend_on_file.depends = path_to_your_txt_file
depend_on_file.CONFIG += recursive
QMAKE_EXTRA_TARGETS += depend_on_file
PRE_TARGETDEPS += depend_on_file
Rebuild will be triggered if file path_to_your_txt_file changes but keep in mind that if your qmake script that parses the file changes DEFINES variable then you out of luck. Defines are not listed in dependencies, you see. If defines are changed you have to do full rebuild by hand.

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

How to use QMake's subdirs template?

I'm starting to learn Qt. I'm moving from the Visual Studio world and I am looking for a way to organize my project's structure using QMake. I've found the 'subdirs' template but I have quite a hard time understanding it.
My project structure looks like this:
project_dir/
main.cpp
project.pro
logic/
logic.pro
some logic files
gui/
gui.pro
gui files
My project.pro looks like this
TEMPLATE = subdirs
SUBDIRS = logic \
gui
SOURCES += main.cpp
In the .pro files for the subdirectories I have appropriate SOURCES, HEADERS and RESOURCES variables set.
Please tell me what TARGET, TEMPLATE and other necessary values I should set in the .pro files.
Also, is there some good QMake tutorial other than the official one?
In addition to Troubadour's comment, I would note that the SUBDIRS target is only good for specifying subdirectories. Therefore, your extra line of
SOURCES += main.cpp
in your project.pro file is incorrect, and will likely fail to build your main.cpp file, at worst. At best, qmake will refuse to parse the file, since it has conflicting specifications in it.
I've used the SUBDIRS template a few times, and it does well if you can build parts into more-or-less independent libraries, apparently like you have with the logic and the gui separate. Here is one way to do this:
project_dir/
-project.pro
-common.pri
-logic/
----logic.pro
----some logic files
-gui/
----gui.pro
----gui files
-build/
----build.pro
----main.cpp
project.pro:
TEMPLATE = subdirs
SUBDIRS = logic \
gui
# build must be last:
CONFIG += ordered
SUBDIRS += build
common.pri:
#Includes common configuration for all subdirectory .pro files.
INCLUDEPATH += . ..
WARNINGS += -Wall
TEMPLATE = lib
# The following keeps the generated files at least somewhat separate
# from the source files.
UI_DIR = uics
MOC_DIR = mocs
OBJECTS_DIR = objs
logic/logic.pro:
# Check if the config file exists
! include( ../common.pri ) {
error( "Couldn't find the common.pri file!" )
}
HEADERS += logic.h
SOURCES += logic.cpp
# By default, TARGET is the same as the directory, so it will make
# liblogic.a (in linux). Uncomment to override.
# TARGET = target
gui/gui.pro:
! include( ../common.pri ) {
error( "Couldn't find the common.pri file!" )
}
FORMS += gui.ui
HEADERS += gui.h
SOURCES += gui.cpp
# By default, TARGET is the same as the directory, so it will make
# libgui.a (in linux). Uncomment to override.
# TARGET = target
build/build.pro:
TEMPLATE = app
SOURCES += main.cpp
LIBS += -L../logic -L../gui -llogic -lgui
# Will build the final executable in the main project directory.
TARGET = ../project
You use subdirs if the logic and gui folders actually repesent some sort of target, eg. a library, that can be built independently of anything else. If that's the case then just use
TEMPLATE = lib
TARGET = logic
CONFIG += dll
in logic.pro.
If they are not independent targets but are just folders that exist to organise the sources files then you can just use a .pri file in each instead and include them within the .pro using
include(logic/logic.pri)
include(gui/gui.pri)
Just remember that the file paths in the .pri files are relative to the .pro file and not the .pri. BTW, the use of a .pri file is optional as you can still list the files in those folders directly in the .pro file. The .pri file just makes it that bit neater and helps keep the .pro file shorter.

Resources