本文整理汇总了Python中PyQt5.Qt.QFontMetrics.boundingRect方法的典型用法代码示例。如果您正苦于以下问题:Python QFontMetrics.boundingRect方法的具体用法?Python QFontMetrics.boundingRect怎么用?Python QFontMetrics.boundingRect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.Qt.QFontMetrics
的用法示例。
在下文中一共展示了QFontMetrics.boundingRect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LayoutItem
# 需要导入模块: from PyQt5.Qt import QFontMetrics [as 别名]
# 或者: from PyQt5.Qt.QFontMetrics import boundingRect [as 别名]
class LayoutItem(QWidget):
def __init__(self, button, parent=None):
QWidget.__init__(self, parent)
self.mouse_over = False
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.button = button
self.text = button.label
self.setCursor(Qt.PointingHandCursor)
self.fm = QFontMetrics(self.font())
self._bi = self._di = None
@property
def bright_icon(self):
if self._bi is None:
self._bi = self.button.icon().pixmap(ICON_SZ, ICON_SZ)
return self._bi
@property
def dull_icon(self):
if self._di is None:
self._di = self.button.icon().pixmap(ICON_SZ, ICON_SZ, mode=QIcon.Disabled)
return self._di
def event(self, ev):
m = None
et = ev.type()
if et == ev.Enter:
m = True
elif et == ev.Leave:
m = False
if m is not None and m != self.mouse_over:
self.mouse_over = m
self.update()
return QWidget.event(self, ev)
def sizeHint(self):
br = self.fm.boundingRect(self.text)
w = max(br.width(), ICON_SZ) + 10
h = 2 * self.fm.lineSpacing() + ICON_SZ + 8
return QSize(w, h)
def paintEvent(self, ev):
shown = self.button.isChecked()
ls = self.fm.lineSpacing()
painter = QPainter(self)
if self.mouse_over:
tool = QStyleOption()
tool.rect = self.rect()
tool.state = QStyle.State_Raised | QStyle.State_Active | QStyle.State_MouseOver
s = self.style()
s.drawPrimitive(QStyle.PE_PanelButtonTool, tool, painter, self)
painter.drawText(
0, 0,
self.width(),
ls, Qt.AlignCenter | Qt.TextSingleLine, self.text)
text = _('Hide') if shown else _('Show')
f = self.font()
f.setBold(True)
painter.setFont(f)
painter.drawText(
0, self.height() - ls,
self.width(),
ls, Qt.AlignCenter | Qt.TextSingleLine, text)
x = (self.width() - ICON_SZ) // 2
y = ls + (self.height() - ICON_SZ - 2 * ls) // 2
pmap = self.bright_icon if shown else self.dull_icon
painter.drawPixmap(x, y, pmap)
painter.end()