Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FontAwesome in Qt

FontAwesome makes adding editable icons into HTML fairly easy:

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

<i class="fa fa-camera-retro"></i>

I have tried adding a FontAwesome icon to a Qt widget, but it doesn't show:

QPushButton *qB = new QPushButton("TXT");
qB -> setProperty("class", "fa fa-camera-retro");

How can I go about it to make it work with Qt widgets?

like image 411
Program-Me-Rev Avatar asked Mar 12 '17 01:03

Program-Me-Rev


People also ask

What is qtawesome in Qt?

QtAwesome is a simple library that can be used to add Font Awesome icons to your Qt application. NOTE: Though the name is QtAwesome and currently it's very Font Awesome based, you can use every other icon/glyph font you want. The class can also be used to manage your own dynamic code-drawn icons, by adding named icon-painters.

Is it possible to use fontawesome with QML?

I wanted to use FontAwesome (an icon set) with QML, but I didn’t want to add the FontAwesome files into my actual QT workspace, for several reasons. One of which is that this is something I would prefer to use a package manager, like NPM, for.

How to add fontawesome to a QRC file?

You need to create a qrc file and bundle FontAwesome to your project, like this: Then include it in the .pro file: Then load it and use it, like this, providing the unicode character of the icon you intend to display: if (QFontDatabase::addApplicationFont (":/FontAwesome.otf") < 0) qWarning () << "FontAwesome cannot be loaded !";

What are the default options available in qtawesome?

The following options are default in the QtAwesome class. When creating an icon, it first populates the options-map with the default options from the QtAwesome object. After that the options are expanded/overwritten by the options supplied to the icon. It is possible to use another glyph per icon-state.


Video Answer


2 Answers

Qt doesn't work like that. You need to create a qrc file and bundle FontAwesome to your project, like this:

<RCC>
  <qresource prefix="/">
    <file alias="FontAwesome.otf">FontAwesome.otf</file>
  </qresource>
</RCC>

Then include it in the .pro file:

RESOURCES += resources.qrc

Then load it and use it, like this, providing the unicode character of the icon you intend to display:

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");

In your case, the camera icon code is indicated here

Result:

enter image description here

like image 70
Massimo Callegari Avatar answered Oct 22 '22 17:10

Massimo Callegari


I'm looking to do the same with Qt5 and the last version of FontAwesome 5.x. After some research, I found a very simple class in GitHub for using Font Icon in Qt 5 and Qt 6 projects with QWidget. This class provides helpers to use Font awesome (v4.7, v5.15 and v6.0) and Google Material Icon (v4).

The code is very small and provides all the functions needed to display icons in QPushButton, QToolButton, etc.

Here, the GitHub link : https://github.com/philvl/ZFontIcon.

like image 35
Alberto H Avatar answered Oct 22 '22 18:10

Alberto H