如何在 QML 中使用 Font Awesome [英] How to use Font Awesome in QML

查看:28
本文介绍了如何在 QML 中使用 Font Awesome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在 QML 中使用 Font Awesome 吗?我找不到如何在 QML 中使用 Font Awesome 的任何文档或任何信息.

Does anyone knows how to use Font Awesome in QML? I couldn't find any document or any information how to use Font Awesome in QML.

推荐答案

我喜欢做的是使用 fontello创建最小的图标集,而不是从 FontAwesome 下载整个图标集.使用 texteditor 示例作为参考:

What I like to do is use fontello to create a minimal set of icons, rather than downloading the whole set from FontAwesome. Using the texteditor example as a reference:

  1. 下载字体并将其存储在项目目录中的某个位置.在示例中,它位于 fonts 文件夹中.
  2. 如果您在项目中使用 .qrc 文件,请将其添加到 其中之一.
  3. 有两种方法可以让你的 QML 中识别字体:FontLoaderQFontDatabase::addApplicationFont().要使用 QFontDatabase::addApplicationFont(),请添加 this 代码在加载应用程序的 QML 之前:

  1. Download the font and store it somewhere in your project directory. In the example, it's in the fonts folder.
  2. If you're using .qrc files in your project, add it to one of those.
  3. There are two ways that I can think of to have the font recognised in your QML: FontLoader and QFontDatabase::addApplicationFont(). To use QFontDatabase::addApplicationFont(), add this code before loading your application's QML:

QFontDatabase fontDatabase;
if (fontDatabase.addApplicationFont(":/fonts/fontello.ttf") == -1)
    qWarning() << "Failed to load fontello.ttf";

  • text<中使用 Unicode 代码/code> 任何项目你想在其中显示图标:

  • Use the Unicode codes in the text property of whatever item you want to display the icon in:

    ToolButton {
        id: openButton
        text: "uF115" // icon-folder-open-empty
        font.family: "fontello"
        onClicked: openDialog.open()
    }
    

  • 这篇关于如何在 QML 中使用 Font Awesome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆