【问题标题】:PySide moving QGraphicsPixmapItem jumps to upper left corner of scenePySide 移动 QGraphicsPixmapItem 跳转到场景左上角
【发布时间】:2012-06-19 08:52:49
【问题描述】:

我正在编写一个应用程序,允许用户通过单击空白区域将图像放置在 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() 

任何帮助将不胜感激!

【问题讨论】:

    标签: qt pyqt pyside qgraphicsview qgraphicsscene


    【解决方案1】:

    您应该在覆盖鼠标释放事件时调用 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() 
    

    【讨论】:

    • 不客气,您也可以将答案标记为正确,而不是将问题重新授权为 SOLVED :)
    • 感谢您的提示 - 我显然是新手!
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多