如何在Qt中使用FontAwesome

Pro*_*Rev 7 c++ qt font-awesome

FontAwesome使得将可编辑的图标添加到HTML中非常简单:

<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">

<i class="fa fa-camera-retro"></i>
Run Code Online (Sandbox Code Playgroud)

我尝试将FontAwesome图标添加到Qt小部件,但它没有显示:

QPushButton *qB = new QPushButton("TXT");
qB -> setProperty("class", "fa fa-camera-retro");
Run Code Online (Sandbox Code Playgroud)

我怎样才能使它与Qt小部件一起使用?

Mas*_*ari 17

Qt不能那样工作.您需要创建一个qrc文件并将FontAwesome捆绑到您的项目中,如下所示:

<RCC>
  <qresource prefix="/">
    <file alias="FontAwesome.otf">FontAwesome.otf</file>
  </qresource>
</RCC>
Run Code Online (Sandbox Code Playgroud)

然后将其包含在.pro文件中:

RESOURCES += resources.qrc
Run Code Online (Sandbox Code Playgroud)

然后加载并使用它,就像这样,提供您要显示的图标的unicode字符:

if (QFontDatabase::addApplicationFont(":/FontAwesome.otf") < 0)
    qWarning() << "FontAwesome cannot be loaded !";

QFont font;
font.setFamily("FontAwesome");
font.setPixelSize(32);

ui->pushButton->setFont(font);
ui->pushButton->setText("\uf083");
Run Code Online (Sandbox Code Playgroud)

在您的情况下,此处显示相机图标代码

结果:

在此输入图像描述

  • 您可以从[Github上的IconFontCppHeaders](https://github.com/juliettef/IconFontCppHeaders)获取定义图标代码点的头文件#include"IconsFontAwesome.h"ui-> pushButton-> setText(ICON_FA_CAMERA_RETRO); (3认同)