How do I access Qt qrc folder directly Qt 5.10.1 - qt

I am developing an application in qml.
I have a folder qrc folder structure like ,
qrc:
Features
- Media
- Audio
- TV
graphics
- media_assets
- audio_assets
- tv_assets
Features contains respective qml files and graphics contains the graphics.
My issue is, after loading a qml file from the features it set as the current directly then, after that when I try to access the graphics folder I need to go back ../.../ and access the folder. It is making problems when folder structure is changed.
How can I access the graphics directly from qrc like graphics/media_assets/ from any qml files
In the older versions I could access the graphics directly by using "file://sample.png", in qt 5.10.1 it is deprecated.

Use:
qrc:///
From url QML Basic Type documentation:
When referring to files stored with the Qt Resource System from within
QML, you should use "qrc:///" instead of ":/" as QML requires URL
paths. Relative URLs resolved from within that file will use the same
protocol.

Related

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

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!

Custom QML Module in the Designer

Since 1 week I try to create a dynamic library which content my qml in resource file. in qmldir I declare my qml files with qrc path for not distribute my qml files to the client.
:
So when I use qrc path in my qmldir, I cannot use the auto-complementation for access to my component and I cannot access to my component form the Designer ...
But when I deploy my application my dll work fine
When I use the relative path, so without qrc://, the auto-complementation working but I cannot access to my component form the Designer ...
And when i deploy my application I NEED to deploy my qml file also of my component library
I the both case I cannot access to my component from the designer.
Maybe I make something wrong when I create my library or I forgot something ...
Any help will be appreciated for my issues...
1 : Can display my custom component in the Designer
2 : Access to auto-complementation without deploy qml
I don't know about designer, never ever used that, but in order to get auto-complete for your custom types, in addition to the qmldir file you must have a plugins.qmltypes file, which pretty much describes the structure of the components.
The good news is it can be auto-generated via:
qmlplugindump My.Module 1.0 /import/path > /import/path/my/module/plugins.qmltypes
From the looks of it, your custom types should appear in designer in their own respective tab when you import the appropriate module and have the necessary support files.

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.

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));

Using Qt Creator in QML Design mode, how to reference an image using qrc path?

So, we have an embedded Linux system running Qt and we compile all of our icons (.png format) into our executable using the resource file. The problem is that I want to be able to use the Qt Creator QML Designer to visually see our screens as we are laying them out, but it only allows me to select a relative file system path (i.e. not a path to the resource). If I go to edit mode and put the qrc:/image.png it works in run time but the image doesn't show up in the QML Design mode. Has anyone ever done this or know if it is possible?
There is at least a workaround:
Put everything in the resource file (the qml files and the icons), and when you'll edit the file in Qt Quick Designer, all paths will be relative so the icons will be visible.
Everything is described there: Managing resource files with the Qt resource system
And to avoid deploying the qml files, you'll have to remove/comment the following line from your .pro:
DEPLOYMENTFOLDERS = folder_01
and replace it with:
OTHER_FILES = <list of qml files>

Resources