psykhi
psykhi

Reputation: 2981

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

Upvotes: 1

Views: 2201

Answers (3)

RobbieE
RobbieE

Reputation: 4350

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.

Upvotes: 1

kikeenrique
kikeenrique

Reputation: 2669

Have you can tried to modify your locale using QLocale?

   QLocale::setDefault(QLocale(QLocale::Basque, QLocale::Spain));

Upvotes: 1

trandatnh
trandatnh

Reputation: 351

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.

Upvotes: 2

Related Questions