def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
self._optionsRects = {}
w, h = self.width(), self.height()
metrics = self.fontMetrics()
hphp = 2 * _hPad
painter.save()
path = QPainterPath()
path.addRoundedRect(.5, .5, w - 1, h - 1, 4, 4)
painter.fillPath(path, QColor(250, 250, 250))
x = 0
linePath = QPainterPath()
for text in self._options[:-1]:
x += hphp + metrics.width(text)
linePath.moveTo(x, 0)
linePath.lineTo(x, h)
pen = painter.pen()
pen.setColor(QColor(218, 218, 218))
pen.setWidth(0)
painter.setPen(pen)
painter.drawPath(path)
painter.setRenderHint(QPainter.Antialiasing, False)
painter.drawPath(linePath)
painter.restore()
painter.translate(_hPad, _vPad + metrics.ascent())
left = 0
for index, text in enumerate(self._options):
if index in self._selection:
color = QColor(20, 146, 230)
else:
color = QColor(63, 63, 63)
painter.setPen(color)
painter.drawText(0, 0, text)
textWidth = metrics.width(text)
rectWidth = textWidth + hphp
rect = (left, 0, rectWidth, h)
self._optionsRects[index] = rect
painter.translate(rectWidth, 0)
left += rectWidth
评论列表
文章目录