简体   繁体   English

如何在 QML 中使用 Font Awesome

[英]How to use Font Awesome in QML

Does anyone knows how to use Font Awesome in QML?有谁知道如何在 QML 中使用 Font Awesome? I couldn't find any document or any information how to use Font Awesome in QML.我找不到任何文档或任何有关如何在 QML 中使用 Font Awesome 的信息。

What I like to do is use fontello to create a minimal set of icons, rather than downloading the whole set from FontAwesome.我喜欢做的是使用fontello创建一组最小的图标,而不是从 FontAwesome 下载整个图标集。 Using the texteditor example as a reference:使用文本编辑器示例作为参考:

  1. Download the font and store it somewhere in your project directory.下载字体并将其存储在项目目录中的某个位置。 In the example, it's in the fonts folder.在示例中,它位于fonts文件夹中。
  2. If you're using .qrc files in your project, add it to one of those .如果您在项目中使用 .qrc 文件,请将其添加到其中一个.qrc 文件中。
  3. There are two ways that I can think of to have the font recognised in your QML: FontLoader and QFontDatabase::addApplicationFont() .我可以想到两种方法来在您的 QML 中识别字体: FontLoaderQFontDatabase::addApplicationFont() To use QFontDatabase::addApplicationFont() , add this code before loading your application's QML:要使用QFontDatabase::addApplicationFont() ,请在加载应用程序的 QML 之前添加以下代码:

     QFontDatabase fontDatabase; if (fontDatabase.addApplicationFont(":/fonts/fontello.ttf") == -1) qWarning() << "Failed to load fontello.ttf";
  4. Use the Unicode codes in the text property of whatever item you want to display the icon in:在要在其中显示图标的任何项目text属性中使用Unicode 代码

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

There is a solution here : https://martin.rpdev.net/2018/03/30/using-fontawesome-5-in-qml.html这里有一个解决方案: https : //martin.rpdev.net/2018/03/30/using-fontawesome-5-in-qml.html

I personnaly mixed it with this solution for FontAwesome 4.7 which gives me this :我个人将它与FontAwesome 4.7 的这个解决方案混合在一起,这给了我这个:

Item {
    id: awesome

    property alias icons: variables

    readonly property FontLoader fontAwesomeRegular: FontLoader {
        source: "./fa-regular-400.ttf"
    }
    readonly property FontLoader fontAwesomeSolid: FontLoader {
        source: "./fa-solid-900.ttf"
    }
    readonly property FontLoader fontAwesomeBrands: FontLoader {
        source: "./fa-brands-400.ttf"
    }

    readonly property string regular: fontAwesomeRegular.name
    readonly property string solid: fontAwesomeSolid.name
    readonly property string brands: fontAwesomeBrands.name

    Awesome.Variables {
        id: variables
    }
}

And Awesome.Variables is another file where I linked the icon names with their character code. Awesome.Variables是另一个文件,我将图标名称与其字符代码链接在一起。 Here is an excerpt :这是摘录:

QtObject {
    readonly property string fa_500px                               : "\uf26e"
    readonly property string fa_accessible_icon                     : "\uf368"
    readonly property string fa_accusoft                            : "\uf369"
    readonly property string fa_address_book                        : "\uf2b9"
    readonly property string fa_address_card                        : "\uf2bb"
}

In order to use it, I load it in my root Item , or in the one where I need an icon like this :为了使用它,我将它加载到我的根Item ,或者在我需要这样的图标的地方加载它:

FontAwesome {
     id: awesome
}

Then use it like this :然后像这样使用它:

Text{
    iconFont: awesome.solid
    text: awesome.icons.fa_arrow_up
}

It's apparently a lot easier today than when the previous answers were posted.今天显然比发布以前的答案时容易得多。

Go to the FontAwesome homepage, search for the icon you want, and download it in SVG format.进入 FontAwesome 主页,搜索您想要的图标,然后以 SVG 格式下载。 Say, bars-solid.svg (a hamburger menu icon).比如说, bars-solid.svg (一个汉堡菜单图标)。

In Qt Creator, add it as say, say, qml.qrc/images/bars-solid.svg .在 Qt Creator 中,将其添加为例如qml.qrc/images/bars-solid.svg

In the QML markup, add it as a property value, say, icon.source: "images/bars-solid.svg"在 QML 标记中,将其添加为属性值,例如icon.source: "images/bars-solid.svg"

If you want QML-only solution for loading a font:如果您想要仅使用 QML 的解决方案来加载字体:

FontLoader {
    source: "fontawsome.ttf"
    Component.onCompleted: console.log(name)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM