load different .qrc file based on screen size - qt

Correct me if i'm wrong but my understanding is all .qrc files that were added to the qt program are loaded into ram when the app runs. Is it possible to load the .qrc files dynamically into ram? For example I have 3 image files one for 720p, one for 1080p, and one for 4k. What I have in my main.cpp is code to get the screen resolution and loads an image from the .qrc. I do not want to put the images onto the local filesystem because of speed and also it is accessible by anyone.
Is there another option to store images files as one file that is loaded into ram based on screen size?

documentation states
Dynamic Resource Loading
A resource can be left out of an application's binary and loaded when it is needed at run-time by using the registerResource() function. The resource file passed into registerResource() must be a binary resource as created by rcc.

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.

Adding resources to QML project results in very slow compilation

I have added some icons to the resource file of my project and suddenly every tiny change to the QML code results in 7-8 seconds of wait time before the project is ran, it almost feels like it is recompiling the C++ code, but in reality that's the delay from the "compilation" of the qrc file.
I tried disabling qrc compression in hopes that it will get faster, but it didn't have any effect. This is very strange and annoying, considering the fact I am using a high end system with a fast CPU and SSD - the extra resources are only 2 megabytes, why is it taking so long? Adding the same files to an archive with high compression takes about 5 milliseconds.
OK, still no clue why is it so slow, but I found a way to avoid it.
The problem was that for every tiny change to QML sources the entire resource file was recompiled. So I moved all the images to another resource file, didn't have to do anything else, no need to change paths or anything, now QML code is in a different resource file and the one with the images is no longer recompiled on every change, so the project launches instantaneously.
Qt's resources system will create a .cpp file that will contain an C++ array of compressed binary data of the files you put there so with 2MB of images + a your qml text file, qrc will have to regenerate a pretty big file that also takes time to compile.
More information here

Which is the best approach while deploying qml files in embeded application?

I am working for an embedded project, UI developed in QML and backend logic developed using DB/other system calls in C++/Qt.
Which is the best approach to deploy qml files?
Does It adding to .qrc (as resource) and compile to executable application?
or
Load QML files from import folder (QML files will be deployed)?
or any other suggesion?
I have around 200 QML files.
QML files in the file system
Files are stored without compression and encryption
Faster to build but slower to deploy
Stored without compression so they take more storage space
Easy to do UI changes on the fly on target (just edit QML and restart)
QML files in the resource file
Resources are compiled to the binary file making the executable file size bigger
Slower to build but faster to deploy.
Takes less storage space because resources are compressed by default.
Because the executable size is bigger the program takes more memory when running.
You can't do changes to UI without re-compiling
See from the link the following chapter Managing Resource Files with the Qt Resource System for an example of relative path referencing.
I don't have any strong opinion but here some of my thoughts:
If memory usage is an issue then you might go without embedded resources and make sure you use dynamic loading of components as much as possible. In the development phase it's easier to go without embedded resources. If it's a customer project I would do the customer delivery as qml files embedded. This prevents the customer from tweaking the UI code themselves.

Distribute Local Video File in Qt

I am developing a Qt application which will have a video playing in the center of the screen. This video is included locally for the application. I am new to Qt development, and thus far I have only seen examples of including resources in the .qrc file.
When I add the video (which is about 2 minutes long), compilation takes approximately 8-10 minutes. Normally it takes between 1-3 seconds to compile.
Is there a way to include a large file (such as a video) to be packaged and distributed with the application?
Adding a large resource like a video to the qrc file carries some overhead, so it's best to avoid using it there. What I would personally do differs from each platform, so I've broken it down:
Windows: ship a .exe installer package with the video file in a folder, so when it's installed the video file is in a specific path relative to the application binary, then load the video file from there.
OS X: ship a .dmg containing the .app bundle, use the QMAKE_BUNDLE_DATA build command to copy the video file into .app bundle, so it's in a specific path relative to the application binary.
Linux: this entirely depends on how you want to distribute, if you're distributing as an installable package (for apt, or yum) then add the video file into the install package, if it's as a .tar.gz then add the video to that.

Flex swf file taking time to load at client side

In my project we have swf file that is 4 mb in size. When we open the application in browser it is taking 3-5 seconds to load the swf file at client side. Is there any way to compress the swf file and send so that it will take less time? Or is there any other options to minimise the load time?
Thanks
Sachin
By default swf files are already compressed when compiled. Try zipping an swf file for example it will hardly change in size.
If you find a compression method to do decrease the size of the swf and this compression method is supported by flash, you can compress the swf file for download and decompress it at runtime and then load it.
Read "Reducing SWF file sizes"
Use the bytecode optimizer
Disable debugging
Use strict mode
Avoid initializing unused classes
Externalize assets
Use character ranges for embedded fonts
Read "Reducing module size"
Change the application that a module is optimized for in Flash Builder
Create and use a linker report with the command-line compiler
Modularize your module
Compress your assets (higher jpg compression, lower color depth, etc)

Resources