I draw a text and question is how to import custom font (tff file in the same folder as script file) and change font. here is the code:
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
self.drawText(e, painter)
super(SpeedPrg, self).paintEvent(e)
def drawText(self, event, qp):
qp.setPen(QtGui.QColor(255,255,255))
qp.drawText(event.rect(), QtCore.Qt.AlignCenter, str(self.setText))
You would use code like this:
# Load the font:
font_db = QFontDatabase()
font_id = font_db.addApplicationFont("your_font.ttf")
# families = font_db.applicationFontFamilies(font_id)
your_ttf_font = QFont("one of your font families")
# tell painter to use your font:
qp.setFont(your_ttf_font)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.