本页内容

FontLoader QML Type

允许通过 URL 加载字体。更多...

Import Statement: import QtQuick

属性

详细说明

FontLoader 类型用于通过 URL 加载字体。

status 用于指示字体何时已加载完毕,这对从远程源加载的字体特别有用。

例如:

import QtQuick 2.0

Column {
    FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" }

    Text { text: "Fancy font"; font: webFont.font }
}

另请参阅 《Qt Quick 示例——文本字体》。

属性文档

font : font [read-only, since 6.0]

该属性保存了已加载字体的默认查询。

如果除了字体家族名称之外还需要其他属性来消除歧义,您可以使用此属性来选择字体。您可以通过单独的属性来指定字体:

Item {
    width: 200; height: 50

    FontLoader {
        id: webFont
        source: "http://www.mysite.com/myfont.ttf"
    }
    Text {
        text: "Fancy font"
        font.family: webFont.font.family
        font.weight: webFont.font.weight
        font.styleName: webFont.font.styleName
        font.pixelSize: 24
    }
}

或者直接设置完整的字体查询:

Item {
    width: 200; height: 50

    FontLoader {
        id: webFont
        source: "http://www.mysite.com/myfont.ttf"
    }
    Text {
        text: "Fancy font"
        font: webFont.font
    }
}

在这种情况下,将直接使用默认字体查询而不作任何修改(例如,字体大小将采用系统默认值)。

该属性在 Qt 6.0 中引入。

name : string [read-only]

该属性存储字体家族的名称。当使用source 属性加载字体时,该属性会自动设置。

这相当于FontLoader 的font 属性中的family属性。

使用此属性可设置Text 项的font.family 属性。

示例:

Item {
    width: 200; height: 50

    FontLoader {
        id: webFont
        source: "http://www.mysite.com/myfont.ttf"
    }
    Text {
        text: "Fancy font"
        font.family: webFont.name
    }
}

source : url

要加载的字体的 URL。

status : enumeration [read-only]

该属性表示字体加载状态。其取值可以是以下之一:

常量描述
FontLoader.Null尚未设置任何字体
FontLoader.Ready字体已加载
FontLoader.Loading字体正在加载中
FontLoader.Error加载字体时发生错误

请使用此状态来提供更新或以某种方式响应状态变化。例如,您可以:

  • 触发状态变更:
    State { name: 'loaded'; when: loader.status == FontLoader.Ready }
  • 实现onStatusChanged 信号处理程序:
    FontLoader {
        id: loader
        onStatusChanged: if (loader.status == FontLoader.Ready) console.log('Loaded')
    }
  • 绑定到状态值:
    Text { text: loader.status == FontLoader.Ready ? 'Loaded' : 'Not loaded' }

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