如何将渐变应用于非确定形状的QGraphicsitem?

2024-09-25 18:19:19 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description hereenter image description here

painterPath = QPainterPath()
painterPath.moveTo(10, 0.0)
painterPath.arcTo(0.0, 0.0, 4.0, 4.0, 90.0, 90.0)
painterPath.lineTo(0.0, 4.0)
painterPath.arcTo(0.0, 2.0, 4.0, 4.0, 180.0, 90.0)
painterPath.lineTo(41.3566,6)
painterPath.lineTo(41.3566+(70.6468*self.grid)*math.cos(self.angle),6+(70.6468*self.grid)*math.sin(self.angle))
painterPath.lineTo(41.3566+(70.6468*self.grid)*math.cos(self.angle)+39.0491,6+(70.6468*self.grid)*math.sin(self.angle))
painterPath.arcTo(41.3566 + (70.6468 * self.grid) * math.cos(self.angle) + 39.0491-6,
                   6 + (70.6468 * self.grid) * math.sin(self.angle)-4,4.0, 4.0, 270.0, 90.0)
painterPath.lineTo(41.3566 + (70.6468 * self.grid) * math.cos(self.angle) + 39.0491-2,
                   6 + (70.6468 * self.grid) * math.sin(self.angle)-4)
painterPath.arcTo(41.3566 + (70.6468 * self.grid) * math.cos(self.angle) + 39.0491-6,
                   6 + (70.6468 * self.grid) * math.sin(self.angle)-6,4.0, 4.0, 0.0, 90.0)
painterPath.lineTo(41.3566+(70.6468*self.grid)*math.cos(self.angle)+39.0491,6+(70.6468*self.grid)*math.sin(self.angle)-6)
painterPath.lineTo(41.3566+(70.6468*self.grid)*math.cos(self.angle)+39.0491-36.0874,6+(70.6468*self.grid)*math.sin(self.angle)-6)
painterPath.lineTo((41.3566+(70.6468*self.grid)*math.cos(self.angle)+39.0491-36.0874)-70.5809*math.cos(self.angle),0)
painterPath.closeSubpath()
gradient = QLinearGradient(1, 1, 1, 5)
gradient.setColorAt(0, QColor(Qt.gray))
gradient.setColorAt(0.5, QColor(192, 192, 192, 255))
gradient.setColorAt(1, QColor(Qt.darkGray))
gradient.setColorAt(40, QColor(Qt.gray))
gradient.setColorAt(40.5, QColor(192, 192, 192, 255))
gradient.setColorAt(41, QColor(Qt.darkGray))
self.setPen(QPen(Qt.NoPen))
self.setPath(painterPath)
self.setBrush(QBrush(gradient))

我可以为顶部水平线设置渐变。但是我需要的是为整个图形设置渐变。还有什么方法可以做到吗


Tags: selfmathsincosqtgridanglegradient