PyQt5之QGraphics 005 QPainterPath的字体
如上图,在QGraphicsItem上用QPainterPath的AddText
可以绘画好多字体。
可以用以下代码查看系统支持的字体:
font_database = QFontDatabase()
for font in font_database.families():
print(font)
通过这段程序,本机打印出以下字体:
Aharoni
Andalus
Angsana New
AngsanaUPC
Aparajita
Arabic Typesetting
Arial
Arial Black
Batang
BatangChe
Browallia New
BrowalliaUPC
Calibri
Cambria
Cambria Math
Candara
Comic Sans MS
Consolas
Constantia
Corbel
Cordia New
CordiaUPC
Courier
Courier New
DaunPenh
David
DFKai-SB
DilleniaUPC
DokChampa
Dotum
DotumChe
Ebrima
Estrangelo Edessa
EucrosiaUPC
Euphemia
Fixedsys
Franklin Gothic Medium
FrankRuehl
FreesiaUPC
Gabriola
Gautami
Georgia
Gisha
Gulim
GulimChe
Gungsuh
GungsuhChe
Impact
IrisUPC
Iskoola Pota
JasmineUPC
Kalinga
Kartika
Khmer UI
KodchiangUPC
Kokila
Lao UI
Latha
Leelawadee
Levenim MT
LilyUPC
Lucida Console
Lucida Sans Unicode
Malgun Gothic
Mangal
Marlett
Meiryo
Meiryo UI
Microsoft Himalaya
Microsoft JhengHei
Microsoft New Tai Lue
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft Uighur
Microsoft Yi Baiti
MingLiU
MingLiU-ExtB
MingLiU_HKSCS
MingLiU_HKSCS-ExtB
Miriam
Miriam Fixed
Modern
*n Baiti
MoolBoran
MS Gothic
MS Mincho
MS PGothic
MS PMincho
MS Sans Serif
MS Serif
MS UI Gothic
MV Boli
Narkisim
Nyala
Palatino Linotype
Plantagenet Cherokee
PMingLiU
PMingLiU-ExtB
Raavi
Rod
Roman
Sakkal Majalla
Script
Segoe Print
Segoe Script
Segoe UI
Segoe UI Light
Segoe UI Semibold
Segoe UI Symbol
Shonar Bangla
Shruti
Simplified Arabic
Simplified Arabic Fixed
SimSun-ExtB
Small Fonts
Sylfaen
Symbol
System
Tahoma
Terminal
Times New Roman
Traditional Arabic
Trebuchet MS
Tunga
Utsaah
Vani
Verdana
Vijaya
Vrinda
Webdings
Wingdings
仿宋
宋体
微软雅黑
新宋体
楷体
黑体
之后我们就可以用QFont设置需要的字体,代码如下:
font = QFont()
# 设置字体
font.setFamily("黑体")
# 设置字体大小像素
font.setPixelSize(20)
# 设置字体为粗体
font.setBold(True)
# 设置字体为斜体
font.setItalic(False)
# 设置字体大小
font.setPointSize(50)
# 设置下划线
font.setUnderline(True)
# 设置上划线
font.setOverline(True)
QFont还有好多其它的属性可以设置,可以查看Qt手册。
字体选择完成,可以用QPainterPath绘制出来,如下:
font_path = QPainterPath()
font_path.addText(10, 180, font, "美美的PyQt5")
painter.setPen(Qt.NoPen)
painter.setBrush(gradient)
painter.drawPath(font_path)
这个功能最有趣的地方是可以用一些特殊的字体,绘制出特殊的图形,这个在Cognex的视觉系统中见到。
比如:
通过设置合适的字体,可以做到一些需要的指示符号。
全部代码如下:
"""
QGraphicsItem 的动态波纹线
By Linyoubiao
2020-03-21
"""
from PyQt5.QtWidgets import (QApplication, QGraphicsItem, QGraphicsScene, QGraphicsView)
from PyQt5.QtCore import (QLine, QLineF, QPoint, QPointF, QRect, QRectF, QTimer, QTime, Qt)
from PyQt5.QtGui import (QBrush, QPen, QImage, QPicture, QPixmap,
QPainter, QPainterPath, QColor,
QFont, QFontMetrics, QFontDatabase,
QLinearGradient)
class Shape(QGraphicsItem):
shape_rect = QRectF(QPointF(0, 0), QPointF(100, 100))
def __init__(self):
super(Shape, self).__init__()
self.timer = QTimer()
self.timer.timeout.connect(self.timerEvent)
self.timer.start(100)
self.line_offset = 0
self.setFlag(QGraphicsItem.ItemIsMovable)
def timerEvent(self):
self.line_offset += 1
self.prepareGeometryChange()
def boundingRect(self):
return QRectF(QPointF(0, 0), QPointF(500, 500))
def paint(self, painter, option, widget):
pen = QPen(Qt.black, 2.0, Qt.SolidLine)
pen.setCosmetic(False)
painter.setPen(pen)
painter.drawRect(Shape.shape_rect)
pen.setColor(Qt.green)
pen.setCapStyle(Qt.FlatCap)
pen.setDashPattern((5, 5))
pen.setDashOffset(self.line_offset)
painter.setPen(pen)
painter.drawRect(Shape.shape_rect)
gradient = QLinearGradient(100, 100, 200, 200)
gradient.setColorAt(0, Qt.red)
gradient.setColorAt(1, Qt.green)
font = QFont()
# 设置字体
font.setFamily("Webdings")
# 设置字体大小像素
font.setPixelSize(20)
# 设置字体为粗体
font.setBold(True)
# 设置字体为斜体
font.setItalic(False)
# 设置字体大小
font.setPointSize(70)
# 设置下划线
font.setUnderline(True)
# 设置上划线
font.setOverline(True)
font_path = QPainterPath()
font_path.addText(10, 180, font, "arar")
painter.setPen(Qt.NoPen)
painter.setBrush(gradient)
painter.drawPath(font_path)
class Shape_Scene(QGraphicsScene):
def __init__(self):
super(Shape_Scene, self).__init__()
self.image = QPixmap("c:/pic/lena.jpg")
self.addPixmap(self.image)
self.shape = Shape()
self.addItem(self.shape)
class Shape_View(QGraphicsView):
def __init__(self, parent=None):
super(Shape_View, self).__init__(parent)
self.shape_scene = Shape_Scene()
self.setScene(self.shape_scene)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
font_database = QFontDatabase()
for font in font_database.families():
print(font)
mainView = Shape_View()
mainView.show()
sys.exit(app.exec_())
多谢,美。
上一篇: PyQt5之布局管理
下一篇: 使用requests访问必应在线翻译