I want to declare a global property in a config file and use it in other files. for example declare mainbg in:
Style.qml:
Basically, if you don't need property binding (if you value is a constant and will not need to be notifiable on change) you can define it in a Javascript shared library, like this :
// MyConstants.js
.pragma library
var mainbg = "red";
And use it in QML like this :
import "MyConstants.js" as Constants
Rectangle {
color: Constants.mainbg;
}
But the bad side of this are :
- no strong typing (JS doesn't really know about types) so you could put anything even if it is not a color.
- and if you change mainbg, the Item using it won't be notified about the change and will keep the old value
So if you need type checking and binding/change notify, simply declare your property as a member of the root object in you main.qml, and it will be accessible from everywhere in the QML application, because the property will in fact be directly registered into the Qml Context object, which is global by definition.
Hope it helps.