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?
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.
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.
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 !";
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With