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:
.qrc files?.qrc file? Could be a stupid question as
I'm not fully aware of the Qt Resource System mechanisms..qrc file? I would have my main.qrc file
loading views and my themes.qrc files loading the custom QML
objects.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With