本文整理汇总了Python中PyQt5.Qt.QPainter.fontMetrics方法的典型用法代码示例。如果您正苦于以下问题:Python QPainter.fontMetrics方法的具体用法?Python QPainter.fontMetrics怎么用?Python QPainter.fontMetrics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.Qt.QPainter
的用法示例。
在下文中一共展示了QPainter.fontMetrics方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _generate_image
# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import fontMetrics [as 别名]
def _generate_image(self):
h = 2
w = 1
def draw_char(pos, pen_color):
pix_paint.setPen(QPen(pen_color))
rect = QRect(i * w + i, 1, w, h)
pix_paint.drawRect(rect)
pix_paint.fillRect(rect, pen_color)
# range of tab:
tab_default = 80
text = "m"
px = QPixmap(100, 100)
p = QPainter(px)
fm = p.fontMetrics()
width = fm.width(text)
p.end()
number_chars_in_one_tab = int(tab_default / width)
p_text = self._prepared_text()
split_p_text = p_text.split("N")
pix_list = []
for text in split_p_text:
line_num = 0
i = 0
length = len(text)
if length == 0:
length = 1
pixmap = QImage(
length * w + length, h * 2, QImage.Format_ARGB32_Premultiplied)
pixmap.fill(Qt.white)
pix_paint = QPainter(pixmap)
for char in text:
if char == "c":
draw_char(i, Qt.black)
if char == "t":
for _ in range(0, number_chars_in_one_tab):
draw_char(i, Qt.transparent)
i += 1
if char == "_":
draw_char(i, Qt.transparent)
i += 1
pix_list.append(pixmap)
line_num += 1
pix_paint.end()
# find max width:
width_list = [pix.width() for pix in pix_list]
max_width = max(width_list)
image = QImage(max_width, pix_list[
0].height() * len(pix_list), QImage.Format_ARGB32_Premultiplied)
image.fill(Qt.white)
painter = QPainter(image)
for pix in pix_list:
y = pix_list[0].height() * pix_list.index(pix)
painter.drawImage(0, y, pix)
painter.end()
return image