How do I split out QML files embedded within a DLL? - qt

I have QML file that has been embedded into a dll. I think it was done something like this
How can I embed a Qt resource into a .dll file?
(The second answer).
Is there anyway to split out the QML file to obtain the source code? I am not very familiar with QT framework

If it's embedded via *.qrc, then it's NOT compatible with standard windows/linux (.dll/.so) resource formats. qrc is compiled as xxx_qrc.cpp file and embedded by linker as .obj file with static initialization code. I.e. it's just part of the binary. You can access "contents" of qrc via QFile with "qrc:/." URL. But for that, you have to load DLL with resources embedded in current process, because qrc is hooked up in static initialization (aka DllMain in Windows). Something like:
QLibrary lib("./library.dll");
if (!lib.load())
throw exception(lib.errorString().toStdString());
QFile resource(":/resource.qml");
if (!resource.open(QIODevice::ReadOnly))
throw exception(resource.errorString().toStdString());
resource.copy("./exported.qml");
To explore currently loaded virtual qrc file system tree, you can use QDir(":/"). I guess it's pretty easy to figure out the rest from here.
And of course - you have to be aware what sort of DLLs you are loading into your process, as they may contain arbitrary code that will be executed as you call QLibrary::load!

Related

Switching between qrc and local path for qml files

As far as i know, the qml files could be loaded from local directory path or could be bundled in qrc file and loaded with qrc:/ URI. In debugging phase changing local qml files doesn't need recompilation of qrc file and linking together with main executable, which is fast procedure for try and error fine tuning. But in deploy phase, the qml files should be bundled together as qrc file and link to main C++ Qt application. This is good practice when you want have single executable file, however compiling qrc file and linking it again is time consuming for big projects. is there any way we can switch to qrc or local directory? for example in debug and release mode?
There is many qml components inside the project and all of these are created by URI's like qrc:/componenentname.qml inside another qml files.
So is there any way to interchange these two states in debug and release modes, and keeping qml files without duplicate changes?
All URLs inside QML, if not specified in full, are relative to the current file.
E.g. if a QML file has content like this
Image {
source: "images/foo.png"
}
then the full URL of the image is constructed at runtime based on the base URL of the QML file itself.
I.e. if the QML file itself is qrc://main.qml then the resulting path is qrc://images/foo.png, if the QML file itself is file:///path/to/your/project/main.qml then the resulting image source is file:///path/to/your/project/image/foo.png.
So if you keep URLs relative in your usage inside QML, you can simply switch between resource and local files when loading the primary QML file.
QUrl mainFile = localMode ? QUrl::fromLocalFile("main.qml") : QUrl("qrc://main.qml")
QQuickView view;
view.setSource(mainFile);
Sorry for the old post,
What we did to handle this is use Qts resource search path. We used PySide2 so examples are in python
try:
QDir.addSearchPath('search_path_name', 'path/to/root/resources')
engine.load('search_path_name:/main.qml')
except FileNotFoundError:
try:
import qml # compiled resources using pyside resource compiler
QDir.addSearchPath('search_path_name', ':/')
engine.load('qrc:///main.qml')
except ModuleNotFoundError:
pass
From this point on resources are loaded using QFile("search_path_name:some_file.txt") and it will load from either search path.

Opening/Editing Qt Resource File (.rcc)

I want to translate a program; but its language files (.qm) are in a .rcc file.
The program is not mine, so I haven't got any .qrc file.
Before asking this question, I have searched this site about this issue; but I don't attain anything.
Is there any way to extract/decompile it?
You can take my tool RccExtended - it based on the official Qt resource compiler with additional function to decompile binary resources.
Usage example:
cd \Path\To\MyQtResources\
rcc --reverse
Decompiler will unpack all .rcc files in the current directory, generate .qrc files and make.bat file to compile resources back to the binary format.
There isn't a supported way to decompile it as far as I'm aware, but it's a binary file format that can be read and handled. There's a nodejs example of how to read the file and extract PNGs on github: https://github.com/gcochard/png-extractor. It may be possible to extend that method out for the .qm files.
However there's other issues with attempting to add more translations to a Qt application without having the code, depending on the language you're attempting to add, how the developer has exposed the other languages etc.

Change Qt application icons dynamically when language change event

I am currently working on a Qt app where the language can be changed dynamically.
To translate the strings, I used a QTranslator and overloaded the changeEvent method in each of my widgets, and everything is working fine on this side.
It's a different thing with the resources of the projects. Indeed Qt resources can have a lang attribute in the resource file (.qrc) of the application, but it seems that it is only loaded at the application startup, based on the user's locale which is not what I want. I would like to be able to change these icons dynamically when a LanguageChange event is fired in my code.
I could use rcc files to change the resource file, but it seems that this file will only be loaded at runtime, therefore I won't be able to access the resources in Qt Designer before running the program.
So can I use, let's say, a resource_en.qrc file in my application .pro file so I can set my icons with Qt Designer, and then use my .rcc files at runtime(resource_fr,etc...) to set the resources dynamically? But how could I unregister a .qrc file from the resources and replace it with an .rcc file (if possible)
Hope I made myself clear enough :D
Thank you :)
You can easily load application icon dynamically by using setWindowIcon method. Assuming mainWin is your QMainWindow.
if (lang == en)
mainWin.setWindowIcon(QIcon(":/Resources/icon/en-icon.png"));
else if (lang == vn)
mainWin.setWindowIcon(QIcon(":/Resources/icon/vn-icon.png"));
Hope this can help.
You can load and unload binary resources using the QResource::registerResource() and QResource::unregisterResource()
Dynamic resource loading
As long as the virtual paths inside each resource file is the same, they should be loaded correctly.
Have you can tried to modify your locale using QLocale?
QLocale::setDefault(QLocale(QLocale::Basque, QLocale::Spain));

How to access resource image with fopen?

I use fopen(filename.c_str(), "rb"); in Qt with images resources.
But fopen wants a full path. It doesn't work if my filename is :/images/img.png!
fopen is not part of Qt. Qt's resource system only works with Qt classes. See the Qt documentation of Using Resources in the Application for details.
The Qt classes which work for resources first check if the given filename starts with the resource marker (:) and if so, they look up the resource internally (no physical file is opened!), otherwise, they open the physical file. Note that resources are included in the source code of your application (as a simple array of bytes) during compile time (using the Qt resource compiler) and read using a special resource reader in Qt internally, which can treat the resource file as it would be a physical file, but it still isn't.
What happens if you pass a resource file path (starting with :) to fopen? fopen tries to read a physical directory called : and searches in this directory for the subpath you provided. Of course, no such directory exists on your harddrive, since the file is part of your executable file. (And please don't try to read the resource file manually from within the executable by opening the executable using fopen and seeking to the resource!)
In your concrete situation: Are you trying to read the image using an external image reader? Qt has its own PNG reader. Just call the QImage(QString filename) constructor.

How to decompress compiled qt resource file with rcc extension?

How to decompress compiled qt resource file with rcc extension?
Since the accepted answer did not meet my needs, I thought I'd post my experience in this.
I wrote a node module that will extract PNG files from QT binary rcc files. It is available on npm as png-extractor if you are interested.
rcc is not a file format. It's Qt's resource compiler. It doesn't create a resource file either. It converts the resources specified in your qrc file into a C++ source file and lets C++ compiler compile it into object code to be linked into your app.
Look for qrc_yourproject.cpp in your build directory to see what's produced. Do not try to access the objects/structures defined in the file directly since Qt may change how they are constructed in later versions. Use Qt's resource management calls to do that.

Resources