1

私は、ユーザーが空白の領域をクリックしてQGraphicsScene(QGraphicsView内に含まれる)に画像を配置し、mousemoveeventを使用して画像を移動できるようにするアプリケーションを作成しています。画像は、サブクラス化された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() 

どんな助けでもいただければ幸いです!

4

1 に答える 1

4

マウスリリースイベントをオーバーライドするときは、QtGui.QGraphicsPixmapItem.mouseReleaseEvent(self、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() 
于 2012-06-16T17:29:02.593 に答える