我正在尝试以编程方式生成一个看起来很像这样的指南针小部件:
我希望指南针的每个“切片”都充当与应用程序其余部分交互的按钮。为此,我认为将它们从QAbstractButton
s 中制作出来是最合乎逻辑的,并开始走这条路:
class compassWedge(QAbstractButton):
def __init__(self, start_angle, parent=None):
super().__init__(parent)
self.start = start_angle
self.setFixedSize(550, 550)
self.setMouseTracking(True)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
brush = QBrush()
if self.underMouse():
brush.setColor(Qt.black)
else:
brush.setColor(Qt.white)
pen = QPen(Qt.black)
pen.setWidth(5)
painter.setBrush(brush)
painter.setPen(pen)
painter.drawPie(25, 25, 500, 500, self.start, 45 * 16)
painter.end()
class compassApplet(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(550, 550)
self.wedges = []
for start in range(0, 360 * 16, 45 * 16):
self.wedges.append(compassWedge(start, self))
到目前为止,这在视觉上效果很好:
问题是,对于underMouse()
,考虑了整个 550x550 切片小部件区域。相反,我想检测鼠标何时位于paintEvent
每个切片中生成的像素内,即painter.drawPie(...)
每个对象中创建的饼图区域。
我怎样才能做到这一点,而不必做复杂的几何来检查鼠标位置与饼形区域?