def __init__(self, parent):
self.setAcceptHoverEvents(True)
def boundingRect(self):
adjust = 2.0
return QRectF(-10 - adjust,
-10 - adjust,
23 + adjust,
23 + adjust
) def shape(self):
path.addEllipse(-10, -10, 20, 20)
return path
def paint(self, painter, option, widget):
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.darkGray)
painter.drawEllipse(-7, -7, 20, 20)
if self.isSelected():
painter.
setBrush(QBrush(color
)) painter.
setPen(QPen(Qt.
black,
0)) painter.drawEllipse(-10, -10, 20, 20)
def mousePressEvent(self, event):
self.scene().clearSelection()
self.setSelected(True)
self.update()
self.bringToFront()
class DisplayItem(QGraphicsItem):
def __init__(self, parent):
self.setAcceptHoverEvents(True)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
def boundingRect(self):
adjust = 2.0
return QRectF(-10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust)
def shape(self):
path = QPainterPath()
path.addEllipse(-10, -10, 20, 20)
return path
def paint(self, painter, option, widget):
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.darkGray)
painter.drawEllipse(-7, -7, 20, 20)
gradient = QRadialGradient(-3, -3, 10)
color = QColor(QColor.fromHsv(60, 255, 255))
if self.isSelected():
color = QColor(Qt.white)
painter.setBrush(QBrush(color))
painter.setPen(QPen(Qt.black, 0))
painter.drawEllipse(-10, -10, 20, 20)
def mousePressEvent(self, event):
self.scene().clearSelection()
self.setSelected(True)
self.update()
self.bringToFront()
QGraphicsItem.mousePressEvent(self, event)
To copy to clipboard, switch view to plain text mode
I have abunch of these QGraphicsItems in a QGraphicsScene displayed on screen through a QGraphicsView. The QGraphicsItem normally looks like a yellow circle with a black edge. When I click on the QGraphicsItem, it turns into a white circle with a black edge.
Bookmarks