QGraphicsPixmapItem放置不正确

问题描述

我需要将QGraphicsPixmapItem移过图像左上角的圆圈。也就是说,当我用鼠标抓住圆圈时,我需要图像的左上角跟随圆圈。我将QGraphicsEllipseItem子类化,并重新实现了itemChange方法,但是当我将图像的位置设置为该值时,图像的位置不正确。我应该在我的代码中修改什么?

    import sys
    from PyQt5.QtWidgets import QMainWindow,QApplication,QGraphicsView
    from PyQt5 import QtGui,QtWidgets
    
    class MainWindow(QMainWindow):
        def __init__(self,parent=None):
            super(MainWindow,self).__init__(parent)
    
            self.scene = Scene()
            self.view = QGraphicsView(self)
            self.setGeometry(10,30,850,600)
            self.view.setGeometry(20,22,800,550)
            self.view.setScene(self.scene)
    
    class Scene(QtWidgets.QGraphicsScene):
        def __init__(self,parent=None):
            super(Scene,self).__init__(parent)
            # other stuff here
            self.set_image()
    
        def set_image(self):
            image = Image()
            self.addItem(image)
            image.set_pixmap()
    
    class Image(QtWidgets.QGraphicsPixmapItem):
        def __init__(self,parent=None):
            super(Image,self).__init__(parent)
    
            self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable,True)
    
        def set_pixmap(self):
            pixmap = QtGui.QPixmap("image.jpg")
            self.setPixmap(pixmap)
            self.pixmap_controller = PixmapController(self)
            self.pixmap_controller.set_pixmap_controller()
            self.pixmap_controller.setPos(self.boundingRect().topLeft())
            self.pixmap_controller.setFlag(QtWidgets.QGraphicsItem.ItemSendsScenePositionChanges,True)
    
        def change_image_position(self,position):
            self.setPos(position)
    
    class PixmapController(QtWidgets.QGraphicsEllipseItem):
        def __init__(self,pixmap):
            super(PixmapController,self).__init__(parent=pixmap)
            self.pixmap = pixmap
    
            self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable,True)
            color = QtGui.QColor(0,0)
            brush = QtGui.QBrush(color)
            self.setBrush(brush)
    
        def set_pixmap_controller(self):
            self.setRect(-5,-5,10,10)
    
        def itemChange(self,change,value):
            if change == QtWidgets.QGraphicsItem.ItemPositionChange:
                self.pixmap.change_image_position(value)
            return super(PixmapController,self).itemChange(change,value)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())

解决方法

当图形项具有父项时,其坐标系基于该父项而不是场景。

问题是,当您尝试移动PixmapController时,该移动是在坐标(像素图项)中进行的。当您检查ItemPositionChange时,您是在更改父级位置,但是根据父级坐标系,项目位置始终会更改。

虽然您可以只返回一个空的QPoint(将不会更改项目位置),但这不是一个好选择:您只要放开鼠标并再次开始移动它,则像素图将重置其位置。

解决方案不是设置可移动项标志,而是过滤鼠标的移动,基于单击开始位置计算 delta ,然后使用该delta根据当前项移动父项位置。

class PixmapController(QtWidgets.QGraphicsEllipseItem):
    def __init__(self,pixmap):
        super(PixmapController,self).__init__(parent=pixmap)
        self.pixmap = pixmap

        # the item should *NOT* move
        # self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable,True)
        color = QtGui.QColor(0,0)
        brush = QtGui.QBrush(color)
        self.setBrush(brush)

    def set_pixmap_controller(self):
        self.setRect(-5,-5,10,10)

    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.startPos = event.pos()

    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            delta = event.pos() - self.startPos
            self.parentItem().setPos(self.parentItem().pos() + delta)

如果要使用change_image_position函数,则需要相应地更改这些函数;下面的代码与上面的示例的最后一行做相同的事情:

class Image(QtWidgets.QGraphicsPixmapItem):
    # ...
    def change_image_position(self,delta):
        self.setPos(self.pos() + delta)

class PixmapController(QtWidgets.QGraphicsEllipseItem):
    # ...
    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            delta = event.pos() - self.startPos
            self.pixmap.change_image_position(delta)

提示:不要像这样向QMainWindow添加子窗口小部件,因为在调整窗口大小时不会正确调整其大小。使用self.setCentralWidget(self.view)代替;如果要添加边距,请使用容器QWidget,将其设置为中央窗口部件,添加一个简单的QHBoxLayout(或QVBoxLayout),将视图添加到该布局,然后使用layout.setContentsMargins(left,top,right,bottom) 设置边距。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...