C
Qt Quick Ultralite 字体绑定示例
演示如何在Qt Quick Ultralite中编写字体属性的绑定。
概述
本示例演示了如何为字体属性编写绑定。仅在使用 Spark Font 引擎时才支持绑定功能。示例的用户界面使用滑块控件来调整文本大小,并使用切换控件来切换文本样式和字形渲染质量。


本示例使用了一个字体映射文件,其中定义了以下字体类名称:
- "Roboto"
- "Roboto Light"
- "Roboto Italic"
它将 QmlProject 的MCU.Config.defaultFontFamily属性设置为“Roboto”。
每当绑定被重新评估时,字体现名都会随之更新。有关更多信息,请参阅字体现名映射。
Text { text: "Hello world" // Depending on the 'italicSwitch.checked' value, the font class // name can be "Roboto" or "Roboto Italic". font.italic: italicSwitch.checked font.pixelSize: pixelSizeSlider.value } Text { text: "from Qt." // An alternativele way is to provide a full class name via font.family property. font.family: italicSwitch.checked ? "Roboto Italic" : "Roboto" font.pixelSize: pixelSizeSlider.value }
对于使用相同样式的文本项,可通过共享相同的字体配置(Text.font)来优化内存使用。
Text { id: lightText text: "Hello again world" font.pixelSize: pixelSizeSlider.value // Depending on the 'lightSwitch.checked' value, the font class // name can be "Roboto" or "Roboto Light". font.weight: lightSwitch.checked ? Font.Light : Font.Normal } Text { // To save the memory resources, the font configurations can be shared // between elements. This happens automatically in the emitted cpp code // when font configuration uses only constant values in font.* bindings. font: lightText.font text: "from Qt." }
通过降低字体的渲染质量,可以进一步优化内存使用。使用低质量的字形在运行时所需的内存更少。将字体的quality 属性设置为Font.QualityVeryLow 会生成并渲染像素位深度为1位的Alpha映射,而当设置为Font.QualityVeryHigh 时,像素位深度为8位。
Text { id: qualityText text: "Variable glyph quality" font: Qt.font({ pixelSize: pixelSizeSlider.value, // Depending on the 'qualitySwitch.checked' value, the font // rendering quality changes. quality: qualitySwitch.checked ? Font.QualityVeryHigh : Font.QualityVeryLow }) }
另请参阅font properties 文档。
目标平台
- MCIMX93-EVK
- MIMXRT1050
- MIMXRT1060
- MIMXRT1064
- MIMXRT1170
- STM32F769I
- STM32H750B
- RH850 D1M1A
- Infineon TRAVEO T2G
文件:
在某些 Qt 许可下提供。
了解更多。