Change Qt application icons dynamically when language change event - qt

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

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.

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>

Convert Multiple SWF files to Single EXE

Hi I have an application in flash, I build in ActionScript 3.0 Flash IDE, my application loads some external swfs which mentioned via XML file. Its working fine at the moment. But I need to compile all these external SWFs and xml file into single exe file. How can I compile like this. or how can I code like this?
EDIT: 1
from here : http://page-flip.com/products/pdf-publisher/
You can see an example, the application is build in .net and it import pdf and publish it as flash projector or web based(swf). How is it compiling all the external SWF files.
If you have Flash CS4 you can make use of the mxmlc compiler which has some additional tricks up it's sleeve.
Using the embed tag like this will allow you to embed an entire swf "inside" your swf:
[Embed(source = '../assets/items/9.swf')] public static const ITEM_9:Class;
Then, to instantiate it you simply go:
var mySprite:Sprite = new ITEM_9() as Sprite;
Using this and some clever overloading of your current classes for external loading should allow you to get a single swf (xml files can be embedded in a similar fashion).
Then it's just a matter of using the Publish settings to make Flash spit out an .exe
On an unrelated note, please go back and accept some answers to your questions. It's not very nice not to.
You can try mdm Zinc.
Zinc is really powerful. It lets you package your Flash or Flex in different ways, with lots of native platform hooks.
you can build an AIR application. if you don`t want it to be cross platform, you can build an AIR application with a windows native installer.
Flash > File > Project settings > Windows Projector.
For MAC, choose a MAC projector.
If you are burning to a disc and you need both platforms to work...a good option is to use Toast (if you are on a MAC)...it will hide the files you don't need the user to see, and also hide windows files from MACs and vice versa.
There is an application for Windows called SWFKit, which allows you to package your SWF and external files into one exe file. I had the same problem as you, and this worked a treat for me. Unfortunately you do need to pay for it :( http://www.swfkit.com
Hope this helps,
Will
I would go about it with these steps
create a flex application
embed all of the SWF's and the XML into that application
create a release of the application you just created
open the SWF application with the stand-alone flash player and not with the browser
from the file menu select the option create projector
All of this will result a single EXE file that contains all of the SWF's and the XML file.
You can use a projector or make an windows only AIR project.
Use flajector and forget about your problems

Resources