Using things loaded in Loader is badly typed, I would rather :
First, create a common ancestor Component for all my styles, e.g :
// AbstractStyle.qml
import QtQuick 2.0;
QtObject {
property color textColorStandard;
}
Next, derivate it to create custom styles, e.g :
// StyleA.qml
import QtQuick 2.0;
AbstractStyle {
textColorStandard: "blue";
}
// StyleB.qml
import QtQuick 2.0;
AbstractStyle {
textColorStandard: "green";
}
Then use a strongly typed property in my object that must use a style, e.g:
// main.qml
import QtQuick 2.0
Item {
id: base;
Component { id: compoStyleA; StyleA { } }
Component { id: compoStyleB; StyleB { } }
property AbstractStyle currentStyle : {
var tmp = (Property.UseThemeA ? compoStyleA : compoStyleB); // choose component
return tmp.createObject (base); // instanciate it and return it
}
Rectangle {
color: currentStyle.textColorStandard;
}
}
That way, there are multiple advantages :
- your code doesn't use Strings to identify components, so errors are easier to find and to avoid ;
- you can't affect an item that doesn't inherit your style base class, so you won't encounter "undefined property" errors and won't need ducktyping to figure out which information you have in your style object ;
- you don't have Loader so you won't have to suffer the nasty "context isolation" between inside and outside the Loader ;
- all components will be preloaded at runtime, gaining speed at instanciation, and allowing you to see from the start if there are syntax errors in the Styles, instead of seeing it only when changing current style ;
- last but not least, property auto-completion will work in QtCreator as the actual type will be known by the IDE !
color: myDynamicStyle.item.textColorStandardinstead :) Cheers! - mlvljr