di_zou
12th July 2010, 13:47
I have a class that inherits QGraphicsItem:
class DisplayItem(QGraphicsItem):
def __init__(self, parent):
self.setAcceptHoverEvents(True)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setCacheMode(QGraphicsItem.DeviceCoordinateCa che)
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)
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.
When I click on a different QGraphicsItem, the first item is supposed to revert back to being yellow and the new item is supposed to turn white. This does not happen. The old item stays white and the new item turns white.
However, if I comment out:
self.setCacheMode(QGraphicsItem.DeviceCoordinateCa che)
eevrything works the way it's supposed to. The new clicked on item turns white, and the old one turns back to yellow. I thought in this mode, the cache was regenerated everytime the item changes. So how come my items don't change?
class DisplayItem(QGraphicsItem):
def __init__(self, parent):
self.setAcceptHoverEvents(True)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setCacheMode(QGraphicsItem.DeviceCoordinateCa che)
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)
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.
When I click on a different QGraphicsItem, the first item is supposed to revert back to being yellow and the new item is supposed to turn white. This does not happen. The old item stays white and the new item turns white.
However, if I comment out:
self.setCacheMode(QGraphicsItem.DeviceCoordinateCa che)
eevrything works the way it's supposed to. The new clicked on item turns white, and the old one turns back to yellow. I thought in this mode, the cache was regenerated everytime the item changes. So how come my items don't change?