I'm actually using the solution provided here: https://stackoverflow.com/a/25864815/2425044
I'd like to get rid of the import "MyTheme.js" as Theme;
statement in order to dynamically load a specific theme at runtime (usually chosen by the user).
What I'm currently doing is loading each of my Themes.js
files in a qrc
file:
redTheme.qrc
containsTheme.js
blueTheme.qrc
containsTheme.js
These qrc
files are compiled into external binary resources (rcc
) and loaded from the binary directory, using
registerResource(const QString &rccFileName, const QString &mapRoot = QString())
So far, everything works. The only problem is that I'm stuck with an import
statement in my QML
files:
import "qrc:/redTheme/Theme.js" as Theme
Thus, despite registeringblueTheme.rcc
as a resource, it will never be used.
I was able to make it work, thanks to other threads.
First off, create your themes like this user does, which inherit from
AbstractStyle
, allowing much more flexibility.https://stackoverflow.com/a/25866188/2425044
Our
property
will then be defined by the value returned by aJS
function:componentCreation.js
Let's say you want to change theme when the user clicks on a
Button
:Remember, redTheme.qml and blueTheme.qml are contained in
qrc
files which are themselves compiled intorcc
files.Here's the definition of
changeTheme(const QString&, const QString&)
, which unregisters the old theme and registers the new one:Other threads that have helped me:
https://wiki.qt.io/Qml_Styling
http://www.slideshare.net/BurkhardStubert/practical-qml-key-navigation (begins at slide 34)