我一直在尝试在我的PyQt应用程序中使用FontAwesome中提供的图标。我已经下载了.ttf文件,并使用addApplicationFont方法将字体加载到我的应用程序中。我有一个QToolButton,我想从AwesomeFont中设置一个图标。我无法弄清楚如何从数据库中选择图标。附上参考代码:
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()