Hi, I'm trying to implement a selection marker so the user can see the selected area in a QGraphicsView. The selection is drawed from a mousePressEvent(..) until a mouseReleaseEvent(..). However the QGraphicsItem isn't always cleared before the next selection? Is there a flush() or something else I need to call?
I have attached the code. The following link points to an image showing the selections not being cleared: http://img263.imageshack.us/img263/6339/marker.png. Thanks! Best regards, Mads
import sys, math from PyQt4.QtGui import * from PyQt4.QtCore import * class SelectionMarker(QGraphicsItem): def __init__(self, parent=None): super(SelectionMarker, self).__init__(parent) self.clearSelection() def boundingRect(self): return self.bRect def clearSelection(self): self.bRect = QRectF(0,0,0,0) self.update() def paintSelection(self, c1, c2): # Scene coords w = qAbs(c1.x()-c2.x()) h = qAbs(c1.y()-c2.y()) centerPosX = min(c1.x(), c2.x())+w/2.0 centerPosY = min(c1.y(), c2.y())+h/2.0 self.setPos( QPointF(centerPosX, centerPosY) ) topLeft = self.mapFromScene( QPointF( min(c1.x(), c2.x()), min(c1.y(), c2.y()) ) ) self.bRect = QRectF( topLeft, QSizeF(w, h) ) self.update() def paint(self, painter=None, qStyleOptionGraphicsItem=None, qWidget=None): painter.setOpacity(0.5) painter.fillRect(self.bRect, QBrush(Qt.blue)) painter.drawRect(self.bRect) class MarkerWidget(QGraphicsView): def __init__(self, parent=None): super(MarkerWidget, self).__init__(parent) self.scene = QGraphicsScene(self) self.cm = SelectionMarker() self.scene.addItem(self.cm) self.setScene(self.scene) self.clickedPos = None def mousePressEvent(self, event): self.clickedPos = QPointF(event.pos()) def mouseReleaseEvent(self, event): self.clickedPos = None self.cm.clearSelection() def mouseMoveEvent(self, event): if self.clickedPos: p1 = self.mapToScene( QPoint(self.clickedPos.x(), self.clickedPos.y()) ) p2 = self.mapToScene( QPoint(event.x(), event.y()) ) self.cm.paintSelection(p1, p2) def drawBackground(self,painter, qRectf): sceneRect = self.sceneRect() painter.drawRect(sceneRect) if __name__ == "__main__": app = QApplication(sys.argv) m = MarkerWidget() m.show() app.exec_()
_______________________________________________ PyQt mailing list [email protected] http://www.riverbankcomputing.com/mailman/listinfo/pyqt
