2

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: The screenshot of error enter image description here

EDIT 2: Trying G.M.'s method resulted in the following error : Any Idea why? enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

3

From https://github.com/dridk/QFontIcon download and add the qfonticon.h and qfonticon.cpp files to your project, then create the icons with the following code:

QFontIcon::addFont("/path/your/fonts/{your font}.ttf");
QIcon icon = QFontIcon::icon(0xf2b9);

{your widget}->setIcon(icon);

Example:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPushButton>
#include "qfonticon.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QWidget *centralWidget;
    QGridLayout *gridLayout;

    centralWidget = new QWidget(this);
    gridLayout = new QGridLayout( centralWidget );

    QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");


    for(int i = 0; i < 15; i++){
        QIcon icon = QFontIcon::icon(0xf2b9+i);
        QPushButton *b = new QPushButton();
        b->setIcon(icon);
        gridLayout->addWidget(b);
    }
    this->setCentralWidget(centralWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

enter image description here

More Information: https://github.com/dridk/QFontIcon

I tested it with Qt 5.7 and Qtcreator 4.2

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Lot of errors are showing up : 'Q_DECL_OVERRIDE' does not name a type virtual void paint(QPainter * painter, const QRect& rect, QIcon::Mode mode, QIcon::State state)Q_DECL_OVERRIDE ; /home/adt/Project/Project1/Project2/MainWindow.cpp:12: error: invalid use of incomplete type 'class Ui::MainWindow' ui(new Ui::MainWindow) ^ ^ – theindianphil1 Jan 05 '17 at 11:57
  • @theindianphil1 qt4 or qt5? – eyllanesc Jan 05 '17 at 12:19
  • qt 5.3.2 using qt creator 3.2.1 – theindianphil1 Jan 05 '17 at 12:23
  • This one error only : : error: 'Q_DECL_OVERRIDE' does not name a type virtual void paint(QPainter * painter, const QRect& rect, QIcon::Mode mode, QIcon::State state);Q_DECL_OVERRIDE; ^ – theindianphil1 Jan 05 '17 at 12:27
  • @theindianphil1 I tested it with Qt 5.7 and Qtcreator 4.2 – eyllanesc Jan 05 '17 at 12:28
  • is there any other way, like directly accessing it from the system directory? using unicodes or sort? – theindianphil1 Jan 05 '17 at 13:07
  • how to change the color option for an icon? Tried using : QIcon icon = QFontIcon::icon(0xf2e0,QColor(1,0,1,255)); Still no change. – theindianphil1 Jan 16 '17 at 07:16
0

Try loading the font explicitly with...

int fid = QFontDatabase::addApplicationFont("/usr/share/fonts/fontawesome-webfont.ttf");

You can then query the font family name(s) using...

QFontDatabase::applicationFontFamilies(fid);

In my case the above resulted in the single family name "FontAwesome". That being the case you should then be able to use the font with...

QFont f("FontAwesome");

Note: The above seems to work as far as it goes but specifying a point size for the font isn't working as expected. Not sure why as the ttf file appears to contain the requested glyph sizes.

Edit 1 The QFont as constructed above can be used like any other unicode font. So a QLabel could be created with...

QLabel label;
label.setFont(QFont("FontAwesome"));
label.setText("\uf0fe");    /* f0fe is the code point for fa-plus-square */

Note that the answer provided by @eyllanesc is probably a far better approach. I'm simply adding this edit for completeness sake.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
G.M.
  • 12,232
  • 2
  • 15
  • 18
  • How do you access the icons individually inside fontawesome-webfont.ttf,also can you share your code – theindianphil1 Jan 05 '17 at 13:05
  • I tried what you suggested, but the output is garbage values. I have edited the question so that you can see the output.Any advice what went wrong? – theindianphil1 Jan 06 '17 at 11:38
  • You're using `QFont("FontAwesome")` in both `QLabel`s in your code but the "ADDRESS ICON" text is being displayed as it would for a standard font. That suggests the font isn't being loaded correctly. Check the value being returned from `QFontDatabase::addApplicationFont` -- If it's -1 there's a problem. – G.M. Jan 06 '17 at 12:52
  • its returning ("FontAwesome") itself, corrected code as well, only one of the labels use FontAwesome. Still producing Garbage Values.Updated ScreenShot – theindianphil1 Jan 09 '17 at 07:06
  • Hey, the code you shared in the Edit works.One more small thing, how do i change the colour of these icons to any other colour from the standard colour. – theindianphil1 Jan 16 '17 at 06:42