Using External Binary Resource - c++

I'm on a project that used to have Compiled-in Resources.
Now, the user can choose the theme that he wants to work on. No problems until there, in a little research I started to use the External Binary Resource approach.
My resources were build successfully and the QResource::registerResource("/path/to/myresource.rcc"); returns true.
It is not working properly though. Apparently the compiled-in resource is still there, in the executable. I can't see the different icons stored in my external binary resource.
How do I remove this compiled-in resource? Do I need to do that to work properly?

Assuming you are using a .pro file for your project, you need to remove the resource file from the RESOURCES list. If you still want it to be listed in your project, you can use OTHER_FILES.
Before:
RESOURCES += file1.qrc file2.qrc
After:
RESOURCES += file2.qrc
OTHER_FILES += file1.qrc
If you want to go a step further you can automate the build of qrc files:
RCC_BINARY_SOURCES += file1.qrc
asset_builder.commands = $$[QT_HOST_BINS]/rcc -binary ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT} -no-compress
asset_builder.depend_command = $$[QT_HOST_BINS]/rcc -list $$QMAKE_RESOURCE_FLAGS ${QMAKE_FILE_IN}
asset_builder.input = RCC_BINARY_SOURCES
asset_builder.output = $$OUT_PWD/$$DESTDIR/${QMAKE_FILE_IN_BASE}.qrb
asset_builder.CONFIG += no_link target_predeps
QMAKE_EXTRA_COMPILERS += asset_builder
OTHER_FILES += $$RCC_BINARY_SOURCES

You have to change the way the resources are compiled. By default, every resource file (resources.qrc, for example) included in a Qt project is compiled to C++ code (the qrc_resources.cpp you've probably seen after compiling the project). That makes the resource is compiled and linked with your executable (or library). The Qt for Visual Studio plugin does exactly that: adds a custom build step to every QRC file. Open the properties of the QRC file to take a look (right-click on the QRC file, then Properties):
Command line: "$(QTDIR)\bin\rcc.exe" -name "%(Filename)" -no-compress "%(FullPath)" -o .\GeneratedFiles\qrc_%(Filename).cpp
Outputs: .\GeneratedFiles\qrc_%(Filename).cpp
%(Filename) is, as you can figure out, the extension-less name of the file
To avoid this behaviour just remove the QRC file from the project. Of course, the problem is that you'll have to manually build the .rcc file. You can do it using a script as part of your makefile.
On the other hand, if you're using Visual Studio, you can change the command used to compile it, adding the -binary option to the rcc tool so it compiles to an external file. In this way it will be included in your usual compilation workflow:
Command line: "$(QTDIR)\bin\rcc.exe" -name "%(Filename)" "%(FullPath)" -binary -o "%(Outputs)"
Outputs: $(OutDir)\%(Filename).rcc - it is different from screenshot because I took it from an existing project, use the one in the text to place the RCC file in the same dir of your executable.
Important note: be sure you change the build tool for all the configurations.
If you use a makefile or Qt Creator instead, you can use this as a base to create the needed script.
Hope this can help you.

Related

Using preload-file compiler option for emscripten compiler to load local file in Qt

I am building an Qt Application using WebAssembly. I want to open a local file and read it frin my code which isn't exacty easy with WebAssembly. I tried using the preload-file option (reference) in my .pro file:
CONFIG += preload-file /path/to/file/Settings.ini
However, when compiling my application with WebAssembly, It fails showing my the following error:
Application exit (RuntimeError: Aborted(native code called abort()))
I am currently using Qt 6 with the fitting Emscripten version. My code works when compiling with GCC. Of course, emscripten compiles when removing the file handling stuff from my code. Using a file dialog isn't a suitable option.
I noticed you are using CONFIG += but the emscripten reference says it is a linker flag. Try using instead QMAKE_LFLAGS += --preload-file /path/to/file/Settings.ini.
Personally, I have not tested preload-file option but two other options have been working for me:
The similar embed-file functionality. In the .pro file add QMAKE_LFLAGS += --embed-file ../../cdpcore/Templates/Models#Models to make the folder specified to be available in [working directory]/Models.
Use the Qt Resource system to embed files. Basically add a .qrc file to your project that lists resources to embed into the binary and then into the .pro file add RESOURCES += resources.qrc.

How to build Qt application using static linking

I am coming for Visual Studio word where you just change the project settings to static or dynamic linking so you application will not need framework dlls to run in on another machine. How do I do that in Qt?
I am following http://doc.qt.io/qt-5/windows-deployment.html but it is that helpful.
It says:
cd C:\path\to\Qt
configure -static <any other options you need>
But c:\Qt is a root folder (default in my case) and configure command is not recognized. I went to c:\Qt\5.3 and still the same case. Also what am I suppose to fill for < if anything? (I am not not filling it and hope it doesn’t mess up)
Do you really have to go command line to do that..is that the only way? I also read few say I need to add this line to .pro file
CONFIG += static
But this doesn’t do anything as well.
How do I link statically?
first of all make sure your Qt version is static .
then add CONFIG += static to your .pro file .

Qt Resource file utilization

Here i am describing the problem that i faced with Qt resource .rcc file.
first, When i created the .qrc file in my project it will fit all the resources that is added in the qrc, in to executable binary file.
second, rcc file in Qt used for well and optimize resource utilization and when i create it in my project it is still including all the resources (added in .qrc file) into the executable binary file even rcc file already contains all the resources so, my question is why to use this rcc even if resources are included in executable binary file. Why to include redundancy in project??
it may possible i misinterpret something or i am not aware of some points, please correct me if i wrong.
It's too late for answer, but may be helps anybody.
I've expected similar problem, and used next solution:
if you use QtCreator, just wrap your RESOURCES += xxx with config condition in .pro file, like that:
!realbuild {
RESOURCES += xxx.qrc
}
and set CONFIG+=realbuild to qmake params. What does it given? You may edit your forms with QtCreator's designer, and use resource directly from editor, but it will not be compiled into your target file, resources must be loaded in run-time using QResource::registerResource(). Use can build resources manually, using direct call to rcc tool, or write a simple script, and call it using QMAKE_POST_LINK variable.
Now question is - how to reload resources in run-time?...
There are two options for Qt resources:
include the .qrc in your .pro file with
RESOURCES = myapp.qrc
create an external binary resource file with rcc, then register it at runtime with
QResource::registerResource("/path/to/myresource.rcc");
Don't do both. i.e. if you previously had the .qrc directly included in your .pro, and now want to include it dynamically, remove the RESOURCES line from the project file and do a clean build. External binary resources are not included in your executable if you don't list them in the RESOURCES setting of your project.

Microsoft Visual Studio: Loading resources in Qt application (without plug-in)

We don't have a Qt plug-in installed for MSVS, and it makes me wonder how/whether it is possible to load resources (images, etc) to the application.
Yes, you can load ressources.
Unfortunately, the qrc Editor which create qrc files is part of the Qt Addin for VS...
But you can create this xml file by hands, for the format see here
Once the qrc file created, you have at least two possibilities :
A) Use qmake
Add a reference to your qrc file in your pro file :
RESOURCES = ApplicationResources.qrc
Regenerate your vcproj from your pro by using qmake
qmake -tp vc
B) If you don't generate your vcproj file from your pro file, you can :
Add manually your qrc file in your solution, for example in the following path :
Resource Files/Res/ApplicationResources.qrc
Add the following commands in the properties of the qrc file in visual studio :
command line : $(QTDIR)\bin\rcc.exe -name ApplicationResources res\ ApplicationResources.qrc -o $(IntDir)\qrc__ ApplicationResources.cpp
Description : RCC res/ApplicationResources.qrc
Output : $(IntDir)\qrc__ ApplicationResources.cpp
C) You can also use an external binary resources file
The command line :rcc -binary myresource.qrc -o myresource.rcc
In the application, you have to register the resource file :
QResource::registerResource("/path/to/myresource.rcc");
For using resource file in the source code see the doc
However, like cheez, I also suggest using qmake and pro file and do not edit properties by hand in Visual Studio...
Hope this helps !
Use the qrc executable to generate a cpp file which you can include in your project:
/usr/local/Trolltech/Qt-4.5.1/bin/rcc -name core core/core.qrc -o build/release/core/qrc_core.cc
See http://doc.trolltech.com/4.0/resources.html
However, I strongly suggest using qmake or some other build system to automate this for you.

Retrieve revision number in VS with qmake

My current workflow:
hg update (or whatever one uses to check out a revision)
MyProject.pro → qmake → MyProject.vcproj
Open Visual Studio, edit files
Build project
During the build step, how can I update my config.h header file with information from version control system (e.g. hg id)?
MyProject.vcproj is generated by qmake, so I shouldn't edit it by hand.
You can execute external commands from inside qmake. The easiest way to make the information available in your sources would be to use a define:
HGID = $$system(hg id)
DEFINES += HGID=\\\"$$HGID\\\"
I'm not sure if you can edit an external file from qmake. You could use an external tool, but on Windows you normally don't have things like sed, so it might be a little more problematic.
You can accomplish that using a custom build target and the PRE_TARGETDEPS keyword. Assuming config.h.in has the folowing format:
#define HGID $HGID
You can define a custom build target that will process hgid.h.in and output to hgid.h prior to building your main target as follows:
hgid.target = hgid
hgid.commands = sed s/\\\$$HGID/`hg id`/ hgid.h.in > hgid.h
QMAKE_EXTRA_TARGETS += hgid
PRE_TARGETDEPS += hgid
One opton is to enable the Keyword Extension. Put something like this in your hgrc (or Mercurial.ini if that's your thing):
[extensions]
hgext.keyword=
[keyword]
config.h =
[keywordmaps]
HGREV = {node}
Then in config.h put:
#define HGREV "$HGREV$"
You might need to parse the hex value out of the "$HGREV: deadbeefdeadbeef $" that you'll get, but that's easily done by whatever code is accessing the HGREV define.
In addition to Lukáš Lalinský and goodrone's comment, I'd like to mention that qmake can link directly to the script, not only to it's output. So one can say
DEFINES += REPO_ID=\\\"`./setlocalversion.sh`\\\"
and the script will be freshly executed for every single target.