pyqt4如何导入和更改绘制文本的自定义字体

时间:2016-12-22 11:30:14

标签: python python-2.7 pyqt pyqt4

我绘制文本,问题是如何导入自定义字体(tff文件与脚本文件在同一文件夹中)并更改字体。 这是代码:

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))

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码:

# 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)