如何在 QML 中使用 Font Awesome [英] How to use Font Awesome in QML
问题描述
有人知道如何在 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:
- 下载字体并将其存储在项目目录中的某个位置.在示例中,它位于
fonts
文件夹中. - 如果您在项目中使用 .qrc 文件,请将其添加到 其中之一.
有两种方法可以让你的 QML 中识别字体:
FontLoader
和QFontDatabase::addApplicationFont()
.要使用QFontDatabase::addApplicationFont()
,请添加 this 代码在加载应用程序的 QML 之前:
- Download the font and store it somewhere in your project directory. In the example, it's in the
fonts
folder. - If you're using .qrc files in your project, add it to one of those.
There are two ways that I can think of to have the font recognised in your QML:
FontLoader
andQFontDatabase::addApplicationFont()
. To useQFontDatabase::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屋!