Details
-
Task
-
Resolution: Done
-
P2: Important
-
None
-
None
-
ae6c7a44394d4a5f943f1fc92f4de015ec7bcc96
Description
Background
Currently there are two ways to share global objects between QML components: share the global object as a context property from C++ or create the global object in the application root component where every other component has access to it. Latter approach creates strong coupling between the root component and sub-components and the sub-components can no longer be constructed in isolation because they depend on the properties of the root component. Also, sharing properties through a shared parent isn't really a solution for components libraries, which have no control of the context they have been created in. Context property approach is not a viable solution either for QML component libraries as introducing new context property produces a potential namespace conflicts when existing QML applications have already reserved the name of the context property for it's own use.
Syntax
One possible solution would be to provide a mechanism for sharing global elements between QML components using a special import syntax that would have to be defined under a mandatory namespace. It should be possible to create the shared object on demand, i.e. only when it gets imported from QML.
import libraryimportpath.globalobject versionnumber as Namespace
Item {
Component.onCompleted: console.log(Namespace.property)
}
Use cases
Sharing default font between text elements.
import QtQuick.defaultFont 1.1 as DefaultFont
Text {
font.family: DefaultFont.name
}
Sharing feedback object between button-like widgets.
import QtMobility.feedback.themeEffect 1.1 as ThemeEffect
Button {
onPressed: ThemeEffect.play(ThemeEffect.Sensitive)
}
Sharing screen orientation information between application views.
import QtQuick.orientation 2.0 as Orientation
Window {
rotation: Orientation.orientation == Orientation.Portrait ? 0 : 90
Behavior on rotation { NumberAnimation { .. } }
}
Sharing a model between different visual elements in the application, for example a clock model could be shared by the application status bar, alarm view and screen saver.
import MyApp.clockModel 1.0 as Clock
Statusbar {
DigitalClock {
minutes: Clock.minutes
hours: Clock.hours
}
}