PyQt5:从右上方到左下方绘制矩形

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

是否可以从右上角到左下角绘制矩形?

现在,我有一个代码,该代码使用QGraphicsRectItem类的setRect()方法从左上角绘制到右下角。

def mousePressEvent(self, event):
    # self.parent().mousePressEvent(event)
    super(DiagramScene, self).mousePressEvent(event)

    ...
    ...
    ...

    # global selectionMode
    if self.parent().selectionMode:
        self.pressed = True
        # self.selectionRect.setParentItem(self)
        if not self.released:
            self.sRstart = event.scenePos()
            self.selectionRect.setRect(self.sRstart.x(), self.sRstart.y(), abs(event.scenePos().x() - self.sRstart.x()),
                                       abs(event.scenePos().y() - self.sRstart.y()))
            self.selectionRect.setVisible(True)

但是,由于setRect(x,y,width,height)方法从坐标x和y设置一个具有从左到右的指定宽度和高度的矩形,因此我不能使用相同的方法从右上角到左下角绘制。有没有办法以相反的方式设置矩形?

python user-interface pycharm pyqt5
1个回答
0
投票

我相信您必须根据进一步左移/向上移动来更改输入。试试:

self.selectionRect.setRect(min(self.sRstart.x(), event.scenePos().x()), #either the start or the current position, always the leftmost -> min
                           max(self.sRstart.y(), event.scenePos().y()), ##either the start or the current position, always the topmost -> max
                           abs(event.scenePos().x() - self.sRstart.x()), #width and height stay the same
                           abs(event.scenePos().y() -self.sRstart.y()))
© www.soinside.com 2019 - 2024. All rights reserved.