I wish to use some of the icons in fontawesome (http://fontawesome.io/icons) in my Qt Application, I have extracted the fontawesome-webfont.ttf file into usr/share/fonts.I tried searching online but could n't find any such examples.This is a sample code I have written for extracting an image out of a Resource(not what is required) and also accessing some Qfonts that were existent in Qfont library itself.( i.e courier new in the example below).
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPixmap>
#include <QLabel>
#include <QHBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
centralWidget = new QWidget(this);
gridLayout = new QGridLayout( centralWidget );
mylabel = new QLabel();
mylabel2= new QLabel();
font = new QFont("courier");
addresspic = new QPixmap(":/new/prefix1/address.png");
*addresspic=addresspic->scaled(50,50,Qt::KeepAspectRatio, Qt::FastTransformation);
mylabel->setPixmap(*addresspic);
mylabel2->setTextFormat(Qt::RichText);
mylabel2->setGeometry(QRect(QPoint(100,100),QSize(150, 150)));
mylabel2->setText(" ADDRESS ICON ");
gridLayout->addWidget(mylabel2);
gridLayout->addWidget(mylabel);
font->setItalic(true);
font->setPixelSize(20);
mylabel2->setFont(*font);
// gridLayout->setVerticalSpacing(1);
// gridLayout->setHorizontalSpacing(1);
this->setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
Thanks again
EDIT 2: Trying G.M.'s method resulted in the following error : Any Idea why?
From https://github.com/dridk/QFontIcon download and add the
qfonticon.h
andqfonticon.cpp
files to your project, then create the icons with the following code:Example:
More Information: https://github.com/dridk/QFontIcon
I tested it with Qt 5.7 and Qtcreator 4.2