问题描述
有人知道如何在QML中使用Font Awesome吗?我找不到任何文档或有关如何在QML中使用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()
,请添加此代码,然后再加载您的应用程序的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代码 任何项目 a>您要在以下位置显示图标:
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中使用Awesome字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!