MCPcopy
hub / github.com/PyQt5/PyQt / paintEvent

Method paintEvent

QPushButton/RotateButton.py:61–90  ·  view source on GitHub ↗

绘制事件

(self, event)

Source from the content-addressed store, hash-verified

59 self._animation.setLoopCount(1) # 只循环一次
60
61 def paintEvent(self, event):
62 """绘制事件"""
63 text = self.text()
64 option = QStyleOptionButton()
65 self.initStyleOption(option)
66 option.text = "" # 不绘制文字
67 painter = QStylePainter(self)
68 painter.setRenderHint(QStylePainter.Antialiasing)
69 painter.setRenderHint(QStylePainter.HighQualityAntialiasing)
70 painter.setRenderHint(QStylePainter.SmoothPixmapTransform)
71 painter.drawControl(QStyle.CE_PushButton, option)
72 # 变换坐标为正中间
73 painter.translate(self.rect().center())
74 painter.rotate(self._angle) # 旋转
75
76 # 绘制图片
77 if self._pixmap and not self._pixmap.isNull():
78 w = self.width()
79 h = self.height()
80 pos = QPointF(-self._pixmap.width() / 2, -self._pixmap.height() / 2)
81 painter.drawPixmap(pos, self._pixmap)
82 elif text:
83 # 在变换坐标后的正中间画文字
84 fm = self.fontMetrics()
85 w = fm.width(text)
86 h = fm.height()
87 rect = QRectF(0 - w * 2, 0 - h, w * 2 * 2, h * 2)
88 painter.drawText(rect, Qt.AlignCenter, text)
89 else:
90 super(RotateButton, self).paintEvent(event)
91
92 def enterEvent(self, _):
93 """鼠标进入事件"""

Callers

nothing calls this directly

Calls 4

textMethod · 0.45
drawControlMethod · 0.45
widthMethod · 0.45
heightMethod · 0.45

Tested by

no test coverage detected