如何创建自定义的属性分组并将它们应用于不同的控件? [英] How to create a custom grouping of properties and apply them to different controls?

查看:60
本文介绍了如何创建自定义的属性分组并将它们应用于不同的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮、文本字段、标签等的窗口.这些都共享通用属性,如字体系列、颜色、大小等.我希望能够做的是定义这些属性的分组(称为,说、textStyles.MainTitle 或 textStyles.DescriptiveText 等),其中包括字体系列、大小和粗细、高度和颜色.然后在 QML 文件中我会写一些类似的东西:

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 控件通过实现它们各自的样式来设置样式,例如对于 Button 你必须实现一个 ButtonStyle.

QML controls are styled by implementing their respective styles, for example for Button you have to implement a ButtonStyle.

至于实际的分组",您可以使用 QtObject

As for the actual "grouping" you can just use a QtObject

property QtObject textStyles : QtObject {
    property FontLoader mainTitle : FontLoader {...}
    ....
}

您还可以将样式化组件扩展为专用类型:

You can also extend styled components as dedicated types:

//StyledText.qml

// StyledText.qml

Text {
  font.family: someFont
  font.pixelSize: fontSize
  color: someColor
  font.italic: true
  font.letterSpacing: -1
  ...
}

然后仅使用 StyledText {} 而不是重复设置常规 Text 元素的样式.

And then use that by just StyledText {} instead of repeatedly styling regular Text elements.

我应该将 QtObject 片段放在何处/在哪个文件中?我不明白//StyledText.qml 是什么,或者 FontLoader 是什么.

Where / in what file do I place the QtObject snippet? I don't understand what // StyledText.qml is, or what a FontLoader is.

如果你希望它在你的整个应用程序中可用,你可以把它作为你的根对象的属性放在 main.qml 中,这要归功于动态范围 textStyles将从您项目中的所有其他文件解析.您也可以将整个组件样式放入其中并在项目中共享.

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 scoping textStyles 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 只是您添加到项目中的额外 qml 文件,您可以在现有 qml 文件中实现其主体,然后右键单击 Text并选择重构 -> 将组件移动到单独的文件中.

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 on Text and select refactoring -> move component into separate file.

A FontLoader 只是一个常规的 QML 组件,您可以使用它来加载特定字体并用作文本的字体源.您不必使用它,您也可以使用系统字体中的字体系列.字体加载器对于您与应用程序捆绑在一起的字体很有用.

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.

这篇关于如何创建自定义的属性分组并将它们应用于不同的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆