QT QML resource files do not recompile after changes - qt

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.

Related

Qt/CMake difficulty with new source files

I am using Qt in a way that the Qt-related files are in a separate subdirectory GUI, and I am using CMake file, the relevant part of it shown below. Basically this setup works, but when I add a new file (something like an own widget), the new file compiles OK, but in the linking phase the new object is not found. I used to delete the build subdirectory, and after that everything works fine. So, my question: do I wrong something with CMake? (I quess the symptoms are caused by some caching problem)
include_directories(${Qt5Widgets_INCLUDES} GUI/include main/include)
add_definitions(${Qt5Widgets_DEFINITIONS})
file(GLOB_RECURSE QOBJECT_HEADERS
"GUI/include/*.h"
)
file(GLOB_RECURSE QOBJECT_SOURCES
"GUI/*.cpp"
)
QT5_WRAP_CPP(hdr_moc ${QOBJECT_HEADERS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS} -std=c++11 -Wall")
add_executable(simGUI main/sim_GUI.cpp ${QOBJECT_SOURCES}
${hdr_moc})
# Use the Widgets module from Qt 5.
target_link_libraries(simGUI Qt5::Widgets)
From CMake doc:
We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.
The typical approach is to list all files manually.

How to get the actual qmake build destination within the project file?

I'm trying to determine the build directory in the qmake project file, but failed in all my experiments so far :-(
At first I had a very plain foo.pro as QtCreator generates it for a plain Qt5 gui app with a few source files. Then I added an EXTRA_BINFILES list with some data files. They must be copied in the same directory as the executable foo. Without the copy stuff, it looks like this:
QT += core gui xml webkitwidgets widgets
TARGET = foo
TEMPLATE = app
EXTRA_BINFILES += \
foobar.png \
baz.png
SOURCES += \
main.cpp \
# ...
HEADERS += \
# ...
FORMS += \
# ...
When I build that, I get the foo executable (or foo.exe if you want). Mostly straightforward so far. Now I want to copy the EXTRA_BINFILES alongside this executable. The open question is how to get the destination directory. My best idea so far is adding this:
for(FILE, EXTRA_BINFILES) {
QMAKE_POST_LINK += $$quote($${QMAKE_COPY} $$shell_path($${PWD}/$${FILE}) $$shell_path($${OUT_PWD})$$escape_expand(\n\t))
}
This uses the OUT_PWD variable, which automatically points to where the Makefile is generated. This is nice for some scenarios. However, I have to deal with two different scenarios:
Directly compiling from within QtCreator with mostly out-of-the-box build configs. It creates a new build-foo-desktop-release directory, creates the Makefile there and builds the executable there. In this scenario, everything works fine.
Building from command-line with qmake -makefile /my/projects/foo/foo.pro and make in a temporary fresh build directory. This way it creates the Makefile in directly in that build directory but compiles the executable into a release subdirectory`. This obviously breaks my copy code.
For some reasons, it is not an option to get rid of one of those scenarios. I have to deal with both of them within the same project file. It is also not an option to make very technical/tricky things in the project file. It has to remain mostly as 'straightforward' as it is. Overriding some of qmake's own variables in the qmake command-line call is also probably not an option. This is because of the broader context, which is too extensive to explain here.
Is there an option to get the correct path in both scenarios? Something like OUT_PWD but for the executable itself?
Unfortunately, DESTDIR is empty (and as mentioned, it is not an option to forcefully set it). DESTDIR_TARGET is empty as well (otherwise I could combine it with dirname, which would be barely non-tricky enough).
Any hints?
sub_dir = $$_PRO_FILE_PWD_
sub_dir ~= s,^$$re_escape($$PWD),,
PROJECT_BUILD_TREE = $$clean_path($$OUT_PWD)
PROJECT_BUILD_TREE ~= s,$$re_escape($$sub_dir)$,,

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"

How do I add an entire directory to a QT creator project

I have an existing QT Creator project. I want to add an entire directory to this project. I see that I can right click in the project file browser tree and "Add Existing Files..." However through this dialog box, I can only add individual files. How can I include an entire directory?
The simplest way is to directly edit your .pro file, add HEADERS += mydir/*.h and SOURCES += mydir/*.cpp and the contents of the whole directory will show up in QT Creator. Further reference: http://qt-project.org/doc/qt-5/qmake-project-files.html
Open a terminal, navigate to the folder where you want to have you project file, and then run the command
qmake -project
This will search the current directory and all subdirectories for files with extensions such as .c, .cpp, .h, etc. (the full list is found by typing man qmake).
But keep in mind that it will overwrite your current .pro file if you already have a project set up.
qmake provides a convenient files function for this very purpose. Adding the following line to your project file will add .cpp files inside the src/ directory:
SOURCES += $$files(src/*.cpp)
By default, this is non-recursive. Setting the second parameter to true recursively finds all files:
SOURCES += $$files(src/*.cpp, true)
The files function was introduced since Qt 5.10.
Nowadays you can just right click on project name and select Add existing directory

Can I get qmake -project to add LIBS += .... to my .pro file?

I have a project that uses Qt. So I have "qmake" make my Makefile from the .pro file. But Qmake can also make that .pro file: qmake -project . This worked until I needed to add an external extra library to my project.
I get lots of hits on google that tell me to add LIBS += ... to my project file, but I want to tell qmake -project something that causes it to add it for me. In effect of course I'll be doing it myself, but I don't think that it's proper that I am editing the generated project file.
If for example I add files to the project directory, I'll have to recreate it and add in the library again, or I'll have to manually add the files to the (almost completely computer-generated) project file. I'm now using a script to auto-generate the project file, and then add in the LIBS += directive, but is there a proper way to do this?
When you are developing without the Qt Creator IDE, unless the IDE includes by itself some automatic utilities, you must edit manually the .pro configuration file.
The generated .pro file is a skeleton file which YOU must fill in with the libraries that you need, then the qmake system figures out the other dependencies and compiles your project. It is a essentially a much better version of pkg-config of gtk + Makefiles.
When you add more source and resource files to your project then manually you must add them to the .pro file.
Example:
QT += core gui
TARGET = qtcp1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
miwidget1.cpp \
lcdrange.cpp
HEADERS += mainwindow.h \
miwidget1.h \
lcdrange.h
FORMS += mainwindow.u
Alternately, you can issue qmake -project over and over again, but this can cause some unforseen accidents, since it includes everything that is in the current directory at the time, including the pre-processed files for conversion to standard C++ from QT dialect. This intermediate files must be erased (cleaned), before the remaking the project and the next make or can lead to tricky problems.
Using the official and free QT Creator IDE takes away most of this burden by adding automatically the new data to the .pro file and cleaning loose ends. Some other IDEs like Code::BLocks and Codelite provide some facilities for QT, but not to the level of QT creator. Some prefer to edit the .pro themselves for custom reasons, other like more other styles of IDEs, like Eclipse.
You should test the waters and decide by yourself what fits best to your needs.
ReEdited, to clarify a few things.

Resources