2012-06-16 1 views
1

사용자는 빈 영역을 클릭 한 다음 mousemoveevent를 사용하여 이미지를 이동하여 QGraphicsView (QGraphicsView 내에 포함)에 이미지를 배치 할 수있는 응용 프로그램을 작성하고 있습니다. 이미지는 서브 클래 싱 된 QGraphicsPixmapItem을 사용하여 생성됩니다.PySide moving QGraphicsPixmapItem이 화면의 왼쪽 상단으로 점프합니다.

다음과 같은 문제가 있습니다. 첫 번째 항목 이동 시도가 예상대로 작동합니다. 그러나 모든 후속 이동에 대해 선택된 항목은 즉시 장면의 왼쪽 위 모퉁이로 점프합니다. 코드는 다음과 같습니다.

import sys 
from PySide import QtGui,QtCore 

class TestPixmapItem(QtGui.QGraphicsPixmapItem): 
    def __init__(self, imagename, position, parent=None): 
     QtGui.QGraphicsPixmapItem.__init__(self, parent) 

     px = QtGui.QPixmap(imagename) 
     self.setPixmap(px) 

     self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True) 
     self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True) 

     # set position 
     self.setPos(position.x(),position.y()) 

    def mouseReleaseEvent(self,e): 
     self.setSelected(False) 

    def mouseMoveEvent(self, e): 
     QtGui.QGraphicsPixmapItem.mouseMoveEvent(self, e) 


class GfxScene(QtGui.QGraphicsScene): 
    def __init__(self, parent=None): 
     #build parent user interface 
     super(GfxScene, self).__init__(parent) 

    def mousePressEvent(self, e): 
     if(self.itemAt(e.scenePos().x(),e.scenePos().y()) == None): 
      pixmapItem = TestPixmapItem('test.png',e.scenePos()) 
      self.addItem(pixmapItem) 
     else: 
      QtGui.QGraphicsScene.mousePressEvent(self,e); 


class MainForm(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     super(MainForm, self).__init__(parent) 

     scene = GfxScene(self) 
     scene.setSceneRect(QtCore.QRect(0, 0, 800, 800)) 

     view = QtGui.QGraphicsView() 
     view.setScene(scene) 
     view.setSceneRect(scene.sceneRect()) 
     #view.setGeometry(QtCore.QRect(0, 0, 800, 800)) 
     self.setCentralWidget(view) 


def main(): 
    #This function means this was run directly, not called from another python file. 
    app = QtGui.QApplication.instance() 
    if app == None: 
      app = QtGui.QApplication(sys.argv) 
    myapp = MainForm() 
    myapp.show() 

    sys.exit(app.exec_()) 


if __name__ == '__main__': 
    main() 

아무 도움이됩니다.

답변

3

마우스 출시 이벤트를 오버라이드 (override) 할 경우에 당신은 QtGui.QGraphicsPixmapItem.mouseReleaseEvent (자기, e)를 호출해야합니다, 참조 : http://goo.gl/ChSYP

import sys 
from PySide import QtGui,QtCore 

class TestPixmapItem(QtGui.QGraphicsPixmapItem): 
    def __init__(self, imagename, position, parent=None): 
     QtGui.QGraphicsPixmapItem.__init__(self, parent) 

    px = QtGui.QPixmap(imagename) 
    self.setPixmap(px) 

    self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True) 
    self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True) 

    # set position 
    self.setPos(position.x(),position.y()) 

def mouseReleaseEvent(self,e): 
    self.setSelected(False) 
    QtGui.QGraphicsPixmapItem.mouseReleaseEvent(self, e) // here calling the event 

def mouseMoveEvent(self, e): 
    QtGui.QGraphicsPixmapItem.mouseMoveEvent(self, e) 


class GfxScene(QtGui.QGraphicsScene): 
    def __init__(self, parent=None): 
     #build parent user interface 
     super(GfxScene, self).__init__(parent) 

def mousePressEvent(self, e): 
    if(self.itemAt(e.scenePos().x(),e.scenePos().y()) == None): 
     pixmapItem = TestPixmapItem('test.png',e.scenePos()) 
     self.addItem(pixmapItem) 
    else: 
     QtGui.QGraphicsScene.mousePressEvent(self,e); 


class MainForm(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     super(MainForm, self).__init__(parent) 

    scene = GfxScene(self) 
    scene.setSceneRect(QtCore.QRect(0, 0, 800, 800)) 

    view = QtGui.QGraphicsView() 
    view.setScene(scene) 
    view.setSceneRect(scene.sceneRect()) 
    #view.setGeometry(QtCore.QRect(0, 0, 800, 800)) 
    self.setCentralWidget(view) 


def main(): 
    #This function means this was run directly, not called from another python file. 
    app = QtGui.QApplication.instance() 
    if app == None: 
     app = QtGui.QApplication(sys.argv) 
    myapp = MainForm() 
    myapp.show() 

sys.exit(app.exec_()) 


if __name__ == '__main__': 
    main() 
+0

당신에게 feelingtheblanks 감사 - 완벽하게 작동! – jsaugustyn

+0

당신도 환영합니다. 문제의 답을 다시 붙이기보다는 정확한 답을 표시 할 수 있습니다 :) – NotCamelCase

+0

팁 주셔서 감사합니다 - 나는 분명히 newb입니다! – jsaugustyn