Qt Quick 示例 - 文本
这是一组与文本相关的 QML 示例。

“文本”是一组与文本相关的简短 QML 示例。每个示例都是一个简短的 QML 文件,通常包含或侧重于某种特定类型或功能。您可以运行每个示例并观察其行为。
Hello
“Hello”演示了如何更改和动画化Text 类型的字符间距。它使用顺序动画,首先在三秒内将font.letterSpacing属性从0 动画化至50 ,随后将文本移动到屏幕上的随机位置:
SequentialAnimation on font.letterSpacing {
loops: Animation.Infinite
NumberAnimation {
from: 0
to: 50
easing.type: Easing.InQuad
duration: 3000
}
ScriptAction {
script: {
container.y = (screen.height / 4) + (Math.random() * screen.height / 2)
container.x = (screen.width / 4) + (Math.random() * screen.width / 2)
}
}
}字体
“字体”演示了在Text 类型中使用字体的不同方式。仅通过名称,直接使用font.family属性:
font.family: "Times"或使用 `FontLoader ` 并指定本地字体文件:
FontLoader {
id: localFont
source: "content/fonts/tarzeau_ocr_a.ttf"
}或者最后使用FontLoader 并指定远程字体文件:
FontLoader {
id: webFont
source: "https://www.princexml.com/fonts/steffmann/Starburst.ttf"
}可用字体
“可用字体”演示了如何使用Qt 全局对象和列表视图来显示系统上所有可用的字体。ListView 类型将可用字体列表用作其模型:
model: Qt.fontFamilies()在委托内部,通过 modelData 设置字体家族:
font.family: parent.modelData横幅
“横幅”是一个简单示例,演示了如何使用一行文本控件和NumberAnimation 来创建横幅。
Img 标签
Img 标签展示了如何通过<img> 标签在文本对象中以不同方式显示图片。
文本布局
“文本布局”演示了如何为文本项创建更复杂的布局。本示例使用 onLineLaidOut 处理程序将文本布局为两栏,该处理程序允许您定位和调整每行的大小:
onLineLaidOut: (line) => {
line.width = width / 2 - main.margin
if (line.y + line.height >= height) {
line.y -= height - main.margin
line.x = width / 2 + main.margin
}
if (line.isLast) {
lastLineMarker.x = line.x + line.implicitWidth
lastLineMarker.y = line.y + (line.height - lastLineMarker.height) / 2
}
}© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.