c++qtqt5font-awesomeqicon

How do I access Awesomefonts in Qt on OpenSuse


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


Solution

  • 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