Qt Quick 示例 - 文本

这是有关文本的 QML 示例集。

Text是与文本有关的 QML 小范例集。每个示例都是一个小 QML 文件,通常包含或强调某一特定类型或特征。您可以运行并观察每个示例的行为。

你好

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: "http://www.princexml.com/fonts/steffmann/Starburst.ttf"
    }

可用字体

Available Fonts展示了如何使用Qt 全局对象和列表视图来显示系统中的所有可用字体。ListView 类型使用可用字体列表作为其模型:

        model: Qt.fontFamilies()

在委托中,字体系列是通过 modelData 设置的:

                font.family: parent.modelData

Banner是一个简单的示例,展示了如何使用一排文本类型和NumberAnimation.NET framework 创建一个横幅。

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
            }
        }

示例项目 @ code.qt.io

© 2025 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.