Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between multiple .qrc files at runtime

Tags:

qt

qml

I have several themes folders each containing a .qrc file:

redTheme/
           - File.qml
           - qml.qrc
blueTheme/
           - File.qml
           - qml.qrc

I am currently able to switch between these themes at compile time. This means I need to change my import statement to the theme I want to use.

I would like to know if I could do this in runtime. It would give much more flexibility to the user. Example : user clicks on a Button which triggers a signal and loads another theme (from C++ or QML)

My first interrogation is : should I use .qrc files or QML Modules?

The former loads its content after being called from C++ whereas the latter compels me to use import statements.

This brings me to other questions:

  1. Can I use several .qrc files?
  2. If so, can I "unload" a .qrc file? Could be a stupid question as I'm not fully aware of the Qt Resource System mechanisms.
  3. How would I load another .qrc file? I would have my main.qrc file loading views and my themes.qrc files loading the custom QML objects.
like image 480
Grégoire Borel Avatar asked Oct 27 '25 09:10

Grégoire Borel


1 Answers

Yes, you can, but only by using external resource binaries:

qrc-files can be either compiled into the executable or as .rcc-file. Those rcc-files can be dynamically loaded. See External Binary Resources. These binary resources can be loaded using QResource::registerResource and QResource::unregisterResource.

Example:

//Build the resources using:
rcc -binary redTheme/qml.qrc -o <build_dir>/themes/redTheme.rcc
rcc -binary blueTheme/qml.qrc -o <build_dir>/themes/blueTheme.rcc

//And in your code:
QResource::registerResource("./themes/redTheme.rcc");

//switching the resource:
QResource::unregisterResource("./themes/redTheme.rcc");
QResource::registerResource("./themes/blueTheme.rcc");

Using this mechanism, you can pack each of your themes in a rcc-file and load it depending on the users choice.

Note: You can actually have a "default theme" compiled directly into the application. As soon as you load the rcc-file, it will overwrite all files with same names. As long as all resources look the same (same file-structure), this will work fine. And as soon as you unload the rcc, Qt will switch back to the applications resources.

like image 52
Felix Avatar answered Oct 29 '25 07:10

Felix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!