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

问题描述 投票:0回答:1

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

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

这里是一个最小的示例:

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_()

这是输出的样子:

enter image description here

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

python qgraphicsview pyside2
1个回答
0
投票

draw_rectQRectF是创建项(QGraphicsRectItem)的输入,该项由类似于QGraphicsRectItemaddRect()方法返回,也就是说,它接收信息但不再使用它。想法是使用addRect()移动项目:

pen

如果仍然要使用draw_rect,则必须在项目中再次设置它:

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 self.item_rect = 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),以便QGraphicsItems,QGraphicsView和QGraphicsScene起作用。

© www.soinside.com 2019 - 2024. All rights reserved.