本文整理汇总了Python中PySide.QtGui.QPainter.fontMetrics方法的典型用法代码示例。如果您正苦于以下问题:Python QPainter.fontMetrics方法的具体用法?Python QPainter.fontMetrics怎么用?Python QPainter.fontMetrics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PySide.QtGui.QPainter
的用法示例。
在下文中一共展示了QPainter.fontMetrics方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: paintEvent
# 需要导入模块: from PySide.QtGui import QPainter [as 别名]
# 或者: from PySide.QtGui.QPainter import fontMetrics [as 别名]
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self)
# Рисование цифр в ячейки таблицы
for i in range(self.matrix_size):
for j in range(self.matrix_size):
# Если текущая ячейка относится к дефолтной судоку
if self.def_num_matrix[i][j]:
painter.save()
# painter.setPen()
painter.setBrush(Qt.yellow)
x = i * self.cell_size
y = j * self.cell_size
w, h = self.cell_size, self.cell_size
painter.drawRect(x, y, w, h)
painter.restore()
# TODO: Закомментировано
# Если индекс ячейки под курсором валидный
if 0 <= self.x_highlight_cell < self.matrix_size and 0 <= self.y_highlight_cell < self.matrix_size:
# # Выделение всего столбца и строки пересекающих ячейку под курсором
# painter.save()
# painter.setBrush(Qt.lightGray)
#
# # Выделение строки
# for i in range(self.matrix_size):
# painter.drawRect(i * self.cell_size,
# self.y_highlight_cell * self.cell_size,
# self.cell_size,
# self.cell_size)
#
# # Выделение столбца
# for j in range(self.matrix_size):
# painter.drawRect(self.x_highlight_cell * self.cell_size,
# j * self.cell_size,
# self.cell_size,
# self.cell_size)
#
# painter.restore()
x, y = self.x_highlight_cell, self.y_highlight_cell
# Не подсвечиваем дефолтную ячейку
if not self.def_num_matrix[x][y]:
# Выделение ячейки под курсором
painter.save()
painter.setBrush(Qt.darkYellow)
painter.drawRect(x * self.cell_size,
y * self.cell_size,
self.cell_size,
self.cell_size)
painter.restore()
# Рисование цифр в ячейки таблицы
for i in range(self.matrix_size):
for j in range(self.matrix_size):
num = self.matrix[i][j]
if not num:
continue
num = str(num)
# Алгоритм изменения размера текста взят из http://stackoverflow.com/a/2204501
# Для текущего пришлось немного адаптировать
factor = (self.cell_size / 2) / painter.fontMetrics().width(num)
if factor < 1 or factor > 1.25:
f = painter.font()
point_size = f.pointSizeF() * factor
if point_size > 0:
f.setPointSizeF(point_size)
painter.setFont(f)
x = i * self.cell_size
y = j * self.cell_size
w, h = self.cell_size, self.cell_size
painter.drawText(x, y, w, h, Qt.AlignCenter, num)
# Рисование сетки таблицы
y1, y2 = 0, 0
factor = min(self.width(), self.height()) / self.default_size
size = self.default_pen_size_1
size2 = self.default_pen_size_2
if factor < 1 or factor > 1.25:
size *= factor
if size < self.min_default_pen_size_2:
size = self.min_default_pen_size_2
painter.save()
for i in range(self.matrix_size + 1):
painter.setPen(QPen(Qt.black, size2 if i % 3 == 0 and i and i < self.matrix_size else size))
painter.drawLine(0, y1, self.cell_size * self.matrix_size, y2)
y1 += self.cell_size
y2 += self.cell_size
#.........这里部分代码省略.........
示例2: add_labels_internal
# 需要导入模块: from PySide.QtGui import QPainter [as 别名]
# 或者: from PySide.QtGui.QPainter import fontMetrics [as 别名]
def add_labels_internal(self, gl, render_state, draw_to_canvas, labels):
'''
call to add a list of labels
'''
text_paint = QPainter()
if draw_to_canvas:
text_paint.begin(self.bitmap)
text_paint.setRenderHints(QPainter.Antialiasing)
u = 0
v = 0
line_height = 0
for label in labels:
ascent = 0
descent = 0
measured_text_width = 0
height = 0
width = 0
font_size = label.font_size
while True:
metrics = None
if draw_to_canvas:
mask = 0x000000FF
b = (label.color >> 16) & mask
g = (label.color >> 8) & mask
r = label.color & mask
######################################################################## LINE CHANGED
text_paint.setPen(QColor(0, 0, 0))
#text_paint.setPen(QColor(r, g, b))
# The value 0.75 is hard coded representing phone pixel density
text_paint.setFont(QFont('Veranda', font_size * 0.75))
# Paint.ascent is negative, so negate it.
metrics = text_paint.fontMetrics()
else:
# The value 0.75 is hard coded representing phone pixel density
metrics = QFontMetrics(QFont('Veranda', font_size * 0.75))
ascent = math.ceil(metrics.ascent())
descent = math.ceil(metrics.descent())
measured_text_width = math.ceil(metrics.boundingRect(label.string).width())
height = int(ascent) + int(descent)
width = int(measured_text_width)
# If it's wider than the screen, try it again with a font size of 1
# smaller.
font_size -= 1
if font_size < 0 or width < render_state.screen_width:
break
next_u = 0
# Is there room for this string on the current line?
if u + width > self.strike_width:
# No room, go to the next line:
u = 0
next_u = width
v += line_height
line_height = 0
else:
next_u = u + width
line_height = max(line_height, height)
if (v + line_height > self.strike_height) and draw_to_canvas:
raise Exception("out of texture space.")
v_base = v + ascent
if draw_to_canvas:
text_paint.drawText(int(u), int(v_base), label.string)
label.set_texture_data(width, height, u, v + height, width, -height,
self.texel_width, self.texel_height)
u = next_u
if draw_to_canvas:
text_paint.end()
return v + line_height