How do I access Awesomefonts in Qt on OpenSuse

608 views Asked by At

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

2

There are 2 answers

7
eyllanesc On BEST ANSWER

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

5
G.M. On

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.