默认项将绘制
boundingRect()
选择后,解决方案是覆盖
paint(...)
方法:
class EllipseItem(QGraphicsEllipseItem):
def paint(self, painter, option, widget):
option.state &= ~QStyle.State_Selected
super(EllipseItem, self).paint(painter, option, widget)
class PolygonItem(QGraphicsPolygonItem):
def paint(self, painter, option, widget):
option.state &= ~QStyle.State_Selected
super(PolygonItem, self).paint(painter, option, widget)
所以现在你必须在代码中使用它:
def populate(self):
scene = self.gv.scene()
for i in range(300):
x = random.randint(-1000, 1000)
y = random.randint(-1000, 1000)
r = random.randint(1, 50)
rect = EllipseItem(x, y, r, r)
rect.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
rect.setBrush(QBrush(QColor(255,128,20,128)))
scene.addItem(rect)
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
rect = EllipseItem(10, 20, 20, 20)
rect.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
rect.setBrush(QBrush(QColor(255,0,0,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
scene.addItem(rect)
points = QPolygonF()
points.append(QPointF(10.4, 10.5))
points.append(QPointF(40.2, 60.2))
points.append(QPointF(30.2, 90.2))
points.append(QPointF(10.2, 80.2))
poly = PolygonItem(points)
poly.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
poly.setBrush(QBrush(QColor(255,0,0,128)))
poly.setFlag( QGraphicsItem.ItemIsSelectable )
poly.setFlag( QGraphicsItem.ItemIsMovable )
scene.addItem(poly)