更改矩形的 X 值实际上并不会在 GUI 中移动它

Changing a rectangle's X value does not actually move it in the GUI

我正在使用 PySide2 在 Python 中开发 GUI。我有一个 GraphicsView,我将在其中放置一个图像,我想在该图像上绘制和移动一个多边形。我在 Python 中找到了许多在 PySide、PySide2 或 PyQt 4/5 中简单绘制多边形、圆形等的示例。但是,我一直无法弄清楚为什么我的图形项目在没有删除和重绘的情况下不会随着事件移动。

我正在使用键盘更改 PySide2 上的 X 值 QRectFX 值明显在变化,但矩形实际上并没有移动。

这是一个最小的例子:

from PySide2 import QtCore, QtGui, QtWidgets
from functools import partial

class DebuggingDrawing(QtWidgets.QGraphicsView):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # initialize the scene and set the size
        self._scene = QtWidgets.QGraphicsScene(self)
        self._scene.setSceneRect(0,0,500,500)
        self.setScene(self._scene)

        # make a green pen and draw a 10 wide, 20 high rectangle at x=20, y=30
        self.pen = QtGui.QPen(QtCore.Qt.green, 0)
        self.draw_rect = QtCore.QRectF(20, 30, 10, 20)
        # add the rectangle to our scene
        self._scene.addRect(self.draw_rect, self.pen)

    def move_rect(self, dx: int):
        # method for moving the existing rectangle
        # get the x value
        x = self.draw_rect.x()
        print('x: {} dx: {}'.format(x, dx))
        # use the moveLeft method of QRectF to change the rectangle's left side x value
        self.draw_rect.moveLeft(x + dx)
        self.update()


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()
        self.labelImg = DebuggingDrawing()

        # Get a keyboard shortcut and hook it up to the move_rect method
        next_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence('Right'), self)
        next_shortcut.activated.connect(partial(self.labelImg.move_rect, 1))

        # get the left key shortcut, move_rect one pixel left
        back_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence('Left'), self)
        back_shortcut.activated.connect(partial(self.labelImg.move_rect, -1))

        self.setCentralWidget(self.labelImg)

        self.setMaximumHeight(480)
        self.update()

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    testing = MainWindow()
    testing.show()
    app.exec_()

输出如下:

您显然无法在图像中看到,但即使矩形的 x 值根据我们的打印调用而变化,图像中也没有任何移动。我已经确认这不仅仅是我的眼睛,因为如果我在 move_rect 中绘制新的矩形,它们就会清楚地显示出来。

draw_rect是一个QRectF是创建一个item的输入(QGraphicsRectItem) that is returned by the addRect()方法类似于pen,即它获取信息但不再使用它。想法是使用 setPos():

移动项目
class DebuggingDrawing(QtWidgets.QGraphicsView):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # initialize the scene and set the size
        self._scene = QtWidgets.QGraphicsScene(self)
        self._scene.setSceneRect(0, 0, 500, 500)
        self.setScene(self._scene)

        # make a green pen and draw a 10 wide, 20 high rectangle at x=20, y=30
        pen = QtGui.QPen(QtCore.Qt.green, 0)
        draw_rect = QtCore.QRectF(20, 30, 10, 20)
        # add the rectangle to our scene
        <b>self.item_rect = </b>self._scene.addRect(draw_rect, pen)

    def move_rect(self, dx: int):
        p = self.item_rect.pos()
        p += QtCore.QPointF(dx, 0)
        self.item_rect.setPos(p)

如果你还想使用draw_rect那么你必须在项目中重新设置它:

    self.pen = QtGui.QPen(QtCore.Qt.green, 0)
    self.draw_rect = QtCore.QRectF(20, 30, 10, 20)
    # add the rectangle to our scene
    <b>self.item_rect = </b>self._scene.addRect(self.draw_rect, self.pen)

def move_rect(self, dx: int):
    # method for moving the existing rectangle
    # get the x value
    x = self.draw_rect.x()
    print('x: {} dx: {}'.format(x, dx))
    # use the moveLeft method of QRectF to change the rectangle's left side x value
    self.draw_rect.moveLeft(x + dx)
    <b>self.item_rect.setRect(self.draw_rect)</b>

建议阅读"Graphics View Framework",以便QGraphicsItems、QGraphicsView 和QGraphicsScene 工作。