I have a window with Buttons, TextFields, Labels etc. These all share common properties like font family, color, size etc. What I would like to be able to do is to define a grouping of these properties (called, say, textStyles.MainTitle or textStyles.DescriptiveText etc.) that would include a font family, size and weight, height and color. And then in the QML file I would write something like:
myCustomProperty: textStyles.MainTitle
and this would apply those values to the control. How can I do this?
QML controls are styled by implementing their respective styles, for example for
Button
you have to implement aButtonStyle
.As for the actual "grouping" you can just use a
QtObject
You can also extend styled components as dedicated types:
// StyledText.qml
And then use that by just
StyledText {}
instead of repeatedly styling regularText
elements.If you want it to be available in your entire application, you can put it as a property of your root object in
main.qml
, thanks to dynamic scopingtextStyles
will resolve from every other file in your project. You can put entire component styles in it as well and share in the project.StyledText.qml
is just an extra qml file you add to your project, you can being with just implementing its body in an existing qml file, then rightclick onText
and select refactoring -> move component into separate file.A
FontLoader
is just a regular QML component, you can use it to load specific fonts and use as a font source for text. You don't have to use that, you can use font families from your system fonts as well. The font loader is useful for fonts you bundle with your application.