2012-09-03 17 views
7

Mam pewne trudności z QGraphicsView i QGraphicsScene. Gdy przybliżam/oddalam scenę i tworzę elementy za pomocą mousePressEvent, mam offset w tej pozycji. Jak można tego uniknąć?MousePressEvent, przesunięcie pozycji w QGraphicsView

event.pos() wydaje się być problem ..

from PyQt4 import QtCore, QtGui 

class graphicsItem (QtGui.QGraphicsItem): 
    def __init__ (self): 
     super(graphicsItem, self).__init__() 
     self.rectF = QtCore.QRectF(0,0,10,10) 
    def boundingRect (self): 
     return self.rectF 
    def paint (self, painter=None, style=None, widget=None): 
     painter.fillRect(self.rectF, QtCore.Qt.red) 

class graphicsScene (QtGui.QGraphicsScene): 
    def __init__ (self, parent=None): 
     super (graphicsScene, self).__init__ (parent) 

class graphicsView (QtGui.QGraphicsView): 
    def __init__ (self, parent = None): 
     super (graphicsView, self).__init__ (parent) 
     self.parent = parent 
    def mousePressEvent(self, event): 
     super (graphicsView, self).mousePressEvent(event) 
     item = graphicsItem() 
     position = QtCore.QPointF(event.pos()) - item.rectF.center() 
     item.setPos(position.x() , position.y()) 
     self.parent.scene.addItem(item) 
    def wheelEvent (self, event): 
     super (graphicsView, self).wheelEvent(event) 
     factor = 1.2 
     if event.delta() < 0 : 
      factor = 1.0/factor 
     self.scale(factor, factor) 

class window (QtGui.QMainWindow): 
    def __init__ (self, parent = None) : 
     super (window, self).__init__(parent) 
     self.width = 800 
     self.height = 600 

     self.resize(self.width,self.height) 
     self.mainLayout = QtGui.QVBoxLayout(self) 

     self.view = graphicsView(self) 
     self.scene = graphicsScene(self) 
     self.view.setScene (self.scene) 

     factor = 1 
     self.scene.setSceneRect(0, 0, self.width * factor, self.height * factor) 
     self.view.setMinimumSize(self.width, self.height) 

     self.mainLayout.addWidget(self.view) 

    def show (self): 
     super (window, self).show() 

Odpowiedz

6

reimplement mousePressEvent na scenie, zamiast widoku.

ten sposób, event argumentem będzie QGraphicsSceneMouseEvent, który ma kilka przydatnych funkcji dodatkowych - w tym scenePos, która robi dokładnie to, co chcesz:

class graphicsScene(QtGui.QGraphicsScene): 
    def __init__ (self, parent=None): 
     super(graphicsScene, self).__init__ (parent) 

    def mousePressEvent(self, event): 
     super(graphicsScene, self).mousePressEvent(event) 
     item = graphicsItem() 
     position = QtCore.QPointF(event.scenePos()) - item.rectF.center() 
     item.setPos(position.x() , position.y()) 
     self.addItem(item) 
+1

Lubię to rozwiązanie i będzie go używać samodzielnie. To tak jakby wewnętrznie mieć mousePressEvent w widoku graficznym, a następnie mapToScene pozycję myszy. Ale to jest bardziej eleganckie. – Trilarion

Powiązane problemy