Anvith
Anvith

Reputation: 507

Load FontAwesome in PyQt app

I have been trying to use the icons present in FontAwesome in my PyQt application. I have downloaded the .ttf file, and used addApplicationFont method to load the fonts into my application. I have a QToolButton for which I want set an icon from AwesomeFont. I am not able to figure out, how to pick an icon from the database. Attaching the code for reference:

import sys

from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    css = """
        QToolButton{{
            border: None;
        }}
    """

    def __init__(self):
        super(Window, self).__init__()

        font_id = QtGui.QFontDatabase.addApplicationFont("fontawesome-webfont.ttf")

        if font_id is not -1:
            font_db = QtGui.QFontDatabase()
            self.font_styles = font_db.styles('FontAwesome')
            self.font_families = QtGui.QFontDatabase.applicationFontFamilies(font_id)
            for font_family in self.font_families:
                self.font = font_db.font(font_family, self.font_styles.first(), 24)
        self.home()

    def home(self):
        self.setStyleSheet(self.css.format())

        btn = QtGui.QToolButton(self)
        btn.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        btn.setFont(self.font)
        btn.setText('.....')
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)

        self.show()

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()

Upvotes: 4

Views: 3624

Answers (2)

OnceACurious
OnceACurious

Reputation: 397

You may use QtAwesome instead https://pypi.org/project/QtAwesome/.

Upvotes: 2

Anvith
Anvith

Reputation: 507

Well I am going to put in my solution, in case someone in the future might need it. There is a pypi package called Qtawesome that lets you load fonts with few easy steps.

But if someone does not want to use a 3rd party package, then I have modified my above code with all the missing statements.

import sys
from six import unichr

from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    css = """
        QToolButton{{
            border: None;
        }}
    """

    def __init__(self):
        super(Window, self).__init__()

        font_id = QtGui.QFontDatabase.addApplicationFont("fontawesome-webfont.ttf")

        if font_id is not -1:
            font_db = QtGui.QFontDatabase()
            self.font_styles = font_db.styles('FontAwesome')
            self.font_families = QtGui.QFontDatabase.applicationFontFamilies(font_id)
            for font_family in self.font_families:
                self.font = font_db.font(font_family, self.font_styles.first(), 24)
        self.home()

    def home(self):
        self.setStyleSheet(self.css.format())

        btn = QtGui.QToolButton(self)
        btn.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        btn.setFont(self.font)
        btn.setText(unichr(int('e025', 16)))
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)

        self.show()

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()

Upvotes: 6

Related Questions