Issue
i am trying to use a custom font in pyqt5, and after some searching it became clear i had to use QFontDatabase.addApplicationFont(). so i implemented it and the file seems to have the right path (i renamed the file and the name in my python changed as well). However i get the standard font when i use this code:
Fontdb = QFontDatabase
Fontdb.addApplicationFont("Omega.ttf")
class Centre(QWidget):
def __init__(self):
super().__init__()
x1 = int(Centralwidth * 0.08333)
y1 = int(Centralheight * 0.083333)
self.setGeometry(int(x1-10), int(y1-10), int(Centralwidth-10), int(Centralheight-10))
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen((QPen(QColor(50, 225, 255, 215), 13, Qt.DashLine)))
x1 = int(Centralwidth * 0.08333)
y1 = int(Centralheight * 0.083333)
dottedcircle = QRectF(x1, y1, 640, 640)
Arc1 = dottedcircle.adjusted(20, 20, -20, -20)
Arc2 = dottedcircle.adjusted(25, 25, -25, -25)
Arc3 = dottedcircle.adjusted(45, 45, -45, -45)
Arc4 = dottedcircle.adjusted(40, 40, -40, -40)
Arc5 = dottedcircle.adjusted(50, 50, -50, -50)
painter.drawEllipse(dottedcircle)
painter.setPen((QPen(QColor(50, 175, 255, 200), 40)))
painter.drawArc(Arc1, 1440, 3200)
painter.setPen((QPen(QColor(50, 150, 230, 190), 30)))
painter.drawArc(Arc2, 5120, 1440)
painter.setPen((QPen(QColor(50, 175, 230, 150), 25)))
painter.drawArc(Arc3, 2240, 1600)
painter.setPen((QPen(QColor(50, 180, 255, 140), 20)))
painter.drawArc(Arc4, 4480, 1520)
painter.setPen((QPen(QColor(50, 160, 255, 210), 50)))
painter.drawArc(Arc5, 800, 900)
painter.drawArc(Arc5, 3680, 900)
painter.setFont(QFont("Omega", 60))
painter.drawText(dottedcircle, Qt.AlignCenter, "A.G.O.S.")
app = QApplication(sys.argv)
window = Centre()
window.show()
sys.exit(app.exec_())
Solution
First of all, you need to ensure that the font is correctly loaded, and that can be easily checked by showing the result of addApplicationFont()
.
fontId = QFontDatabase.addApplicationFont("Omega.ttf")
if fontId < 0:
print('font not loaded')
Then, the argument for the QFont constructor is not the file name, but the font family. You can check them by using the previously returned font id with applicationFontFamilies
using the font id returned by addApplicationFont()
:
families = QtGui.QFontDatabase.applicationFontFamilies(fontId)
font = QtGui.QFont(families[0])
Finally, addApplicationFont
should always be called after the instance of QApplication is created.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.