简体   繁体   中英

pyqt4 how to import and change custom font for painted text

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM