有人知道如何在QML中使用Font Awesome吗?我找不到任何文件或信息来介绍如何在QML中使用Font Awesome。
有人知道如何在QML中使用Font Awesome吗?我找不到任何文件或信息来介绍如何在QML中使用Font Awesome。
fonts
folder.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";
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()
}
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
}
}
而 Awesome.Variables
则是另一个文件,我在其中将图标名称与它们的字符代码进行了关联。以下是一小部分内容:
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"
}
Item
中,或者在需要这样的图标的那个项目中。FontAwesome {
id: awesome
}
Text{
iconFont: awesome.solid
text: awesome.icons.fa_arrow_up
}
相较于之前发布的答案,现在要容易得多。
前往FontAwesome首页,搜索你需要的图标,并以SVG格式下载。比如,bars-solid.svg(汉堡菜单图标)。
在Qt Creator中添加它,命名为qml.qrc/images/bars-solid.svg。
在QML标记中,将其作为一个属性值添加,命名为icon.source: "images/bars-solid.svg"
FontLoader {
source: "fontawsome.ttf"
Component.onCompleted: console.log(name)
}
FontLoader
只是在幕后使用QFontDatabase :: addApplicationFont()
,所以您不能像使用FontLoader
id
一样使用字符串font.family
来使用它吗? - GrecKoid
:http://doc.qt.io/qt-5/qml-qtquick-fontloader.html。 - Mitch