本文整理汇总了Python中PyQt4.QtGui.QFontMetricsF.boundingRect方法的典型用法代码示例。如果您正苦于以下问题:Python QFontMetricsF.boundingRect方法的具体用法?Python QFontMetricsF.boundingRect怎么用?Python QFontMetricsF.boundingRect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtGui.QFontMetricsF
的用法示例。
在下文中一共展示了QFontMetricsF.boundingRect方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: paintEvent
# 需要导入模块: from PyQt4.QtGui import QFontMetricsF [as 别名]
# 或者: from PyQt4.QtGui.QFontMetricsF import boundingRect [as 别名]
def paintEvent(self, event=None):
font = QFont(self.font())
font.setPointSize(font.pointSize() - 1)
fm = QFontMetricsF(font)
fracWidth = fm.width(FractionSlider.WSTRING)
indent = fm.boundingRect("9").width() / 2.0
if not X11:
fracWidth *= 1.5
span = self.width() - (FractionSlider.XMARGIN * 2)
value = self.__numerator / float(self.__denominator)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setRenderHint(QPainter.TextAntialiasing)
painter.setPen(self.palette().color(QPalette.Mid))
painter.setBrush(self.palette().brush(
QPalette.AlternateBase))
painter.drawRect(self.rect())
segColor = QColor(Qt.green).dark(120)
segLineColor = segColor.dark()
painter.setPen(segLineColor)
painter.setBrush(segColor)
painter.drawRect(FractionSlider.XMARGIN,
FractionSlider.YMARGIN, span, fm.height())
textColor = self.palette().color(QPalette.Text)
segWidth = span / self.__denominator
segHeight = fm.height() * 2
nRect = fm.boundingRect(FractionSlider.WSTRING)
x = FractionSlider.XMARGIN
yOffset = segHeight + fm.height()
for i in range(self.__denominator + 1):
painter.setPen(segLineColor)
painter.drawLine(x, FractionSlider.YMARGIN, x, segHeight)
painter.setPen(textColor)
y = segHeight
rect = QRectF(nRect)
rect.moveCenter(QPointF(x, y + fm.height() / 2.0))
painter.drawText(rect, Qt.AlignCenter,
QString.number(i))
y = yOffset
rect.moveCenter(QPointF(x, y + fm.height() / 2.0))
painter.drawText(rect, Qt.AlignCenter,
QString.number(self.__denominator))
painter.drawLine(QPointF(rect.left() + indent, y),
QPointF(rect.right() - indent, y))
x += segWidth
span = int(span)
y = FractionSlider.YMARGIN - 0.5
triangle = [QPointF(value * span, y),
QPointF((value * span) +
(2 * FractionSlider.XMARGIN), y),
QPointF((value * span) +
FractionSlider.XMARGIN, fm.height())]
painter.setPen(Qt.yellow)
painter.setBrush(Qt.darkYellow)
painter.drawPolygon(QPolygonF(triangle))
示例2: selectValues
# 需要导入模块: from PyQt4.QtGui import QFontMetricsF [as 别名]
# 或者: from PyQt4.QtGui.QFontMetricsF import boundingRect [as 别名]
def selectValues(self, length, is_vertical, painter = None):
if painter is not None:
metric = QFontMetricsF(self.font, painter.device())
else:
metric = QFontMetricsF(self.font)
min_dist = 1
if not is_vertical: # Find the maximum size of 2 figures
test = [ str(i)*2 for i in range(10) ]
for t in test:
min_dist = max(min_dist, metric.boundingRect(t).width())
return self._selectValues_direct(length, is_vertical, metric, min_dist)
示例3: draw
# 需要导入模块: from PyQt4.QtGui import QFontMetricsF [as 别名]
# 或者: from PyQt4.QtGui.QFontMetricsF import boundingRect [as 别名]
def draw(self, painter, size = None):
"""
:Arguments:
painter : QPainter
Opened painter on which to draw
"""
bounding_rect = QRectF()
position = self.position
transfer_function = self.transfer_function
font = QFont(self.font)
text_color = self.text_color
line_color = self.line_color
line_thickness = self.line_thickness
value_range = self.value_range
if size is None:
viewport = painter.viewport() # viewport rectangle
mat, ok = painter.worldMatrix().inverted()
if not ok:
raise ValueError("Transformation matrix of painter is singular.")
viewport = mat.mapRect(viewport)
else:
viewport = size
# First, prepare the gradient
w = viewport.width()
h = viewport.height()
#print("Size of viewport: {0}x{1}".format(w, h))
gr = QLinearGradient()
nb_values = ceil(w/5.0)
brush_color = QColor()
for i in range(int(nb_values)):
brush_color.setRgbF(*transfer_function.rgba(i/nb_values))
gr.setColorAt(i/nb_values, brush_color)
# Second, find its position
metric = QFontMetricsF(font, painter.device())
font_test = [ str(i)*5 for i in range(10) ]
lim_width = 0
lim_height = 0
for t in font_test:
rect = metric.boundingRect(t)
lim_width = max(lim_width, rect.width())
lim_height = max(lim_height, rect.height())
lim_height *= 3
length = self.scale_length
shift_length = (1-length)/2
width = self.scale_width
shift_width = self.scale_shift_width
delta_value = value_range[1]-value_range[0]
if position == "Top":
scale_rect = QRectF(shift_length*w, shift_width*h, length*w, width*h)
limit_rect(scale_rect, viewport, lim_width, lim_height)
gr.setStart(scale_rect.left(), scale_rect.center().y())
gr.setFinalStop(scale_rect.right(), scale_rect.center().y())
start_pos = scale_rect.bottomLeft()
end_pos = scale_rect.bottomRight()
elif position == "Right":
scale_rect = QRectF((1-shift_width-width)*w, shift_length*h, width*w, length*h)
limit_rect(scale_rect, viewport, lim_width, lim_height)
gr.setStart(scale_rect.center().x(), scale_rect.bottom())
gr.setFinalStop(scale_rect.center().x(), scale_rect.top())
start_pos = scale_rect.bottomLeft()
end_pos = scale_rect.topLeft()
elif position == "Bottom":
scale_rect = QRectF(shift_length*w, (1-shift_width-width)*h, length*w, width*h)
limit_rect(scale_rect, viewport, lim_width, lim_height)
gr.setStart(scale_rect.left(), scale_rect.center().y())
gr.setFinalStop(scale_rect.right(), scale_rect.center().y())
start_pos = scale_rect.topLeft()
end_pos = scale_rect.topRight()
elif position == "Left":
scale_rect = QRectF(shift_width*w, shift_length*h, width*w, length*h)
limit_rect(scale_rect, viewport, lim_width, lim_height)
gr.setStart(scale_rect.center().x(), scale_rect.bottom())
gr.setFinalStop(scale_rect.center().x(), scale_rect.top())
start_pos = scale_rect.bottomRight()
end_pos = scale_rect.topRight()
else:
raise ValueError("Invalid scale position: %s" % position)
shift_pos = (end_pos-start_pos)/delta_value
if position in ["Left", "Right"]:
is_vertical = True
length = scale_rect.height()
else:
is_vertical = False
length = scale_rect.width()
# Get the ticks
ticks = self.selectValues(length, is_vertical, painter)
if len(ticks) == 0:
return
ticks_str, ticks_extra = self._tick2str(ticks)
# Figure the shifts
dist_to_bar = self.text_to_bar
max_width = 0
max_height = 0
for t in ticks_str:
rect = metric.boundingRect(t)
max_width = max(rect.width(), max_width)
max_height = max(rect.height(), max_height)
if position == "Left":
shift_left = dist_to_bar
shift_top = None
#.........这里部分代码省略.........