I'm looking for a simple way to share read-only values across multiple QML files, for instance; lets say I have a label element:
Label {
id: titleLabel
text: listView.currentItem ? listView.currentItem.text : "IDEAL Networks"
font.pixelSize: 20
elide: Label.ElideRight
horizontalAlignment: Qt.AlignLeft
verticalAlignment: Qt.AlignVCenter
Layout.fillWidth: true
color: red;
padding: {
left: 14
}
}
The color
and padding
values need to be used in other QML files and other areas of the same file.
Rather than re-typing red
and 14 in multiple locations is there a way I can create a shared library containing these values instead to make it easier to update globally at a later date?
* Update *
I've followed the instructions here: http://doc.qt.io/qt-5/qtqml-modules-qmldir.html
However when I import the custom CustomStyles 1.0
module I get an error - module "CustomStyles" is not installed.
//Style.qml with custom singleton type definition
pragma Singleton
import QtQuick 2.0
QtObject {
property int textSize: 20
property color textColor: "green"
}
// qmldir declaring the singleton type
module CustomStyles
singleton Style 1.0 Style.qml
// singleton type in use
import QtQuick 2.0
import CustomStyles 1.0
Text {
font.pixelSize: Style.textSize
color: Style.textColor
text: "Hello World"
}