本页内容

C

Qt Quick Ultralite 字体绑定示例

演示如何在Qt Quick Ultralite中编写字体属性的绑定。

概述

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

字体绑定示例:显示了一个小字号滑块设置,其中禁用了斜体和高质量字形,并启用了浅体样式。

Font_bindings 示例,展示了将字体大小滑块设置为较大值、禁用斜体并启用轻体样式的情况。

本示例使用了一个字体映射文件,其中定义了以下字体类名称:

  • "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 文档。

目标平台

文件:

在某些 Qt 许可下提供。
了解更多。