在QGraphicScene中使用鼠标事件绘制矩形时出现问题。

huangapple go评论51阅读模式
英文:

Problem in drawing Rectangle with mouse event in QGraphicScene

问题

我有一个用于绘制矩形的类,它可以正常工作,但当拖动矩形并且起始点(QPoint)大于终止点时,当前矩形不会显示。

这个剪辑展示了我的问题!
在QGraphicScene中使用鼠标事件绘制矩形时出现问题。

而这是代码:

class RectangleScene(QGraphicsScene):
    def __init__(self, *args, **kwargs):
        super(RectangleScene, self).__init__(*args, **kwargs)
        self.graphics_line = None
        self.start_point = QtCore.QPointF()
        self.end_point = QtCore.QPointF()

    def clean_scene(self):
        for index, item in enumerate(self.items()[:-1]):
            if index > 1:

    def mousePressEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.clean_scene()
            self.start_point = event.scenePos()
            self.end_point = self.start_point
            self.graphics_line = QtWidgets.QGraphicsRectItem(QtCore.QRectF(self.start_point, self.end_point))
            self.addItem(self.graphics_line)
            self.update_path()

        elif QtCore.Qt.RightButton and self.graphics_line:
            self.contextMenuEvent(event)
            super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.end_point = event.scenePos()
            self update_path()
            super(RectangleScene, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
            super(RectangleScene, self).mouseReleaseEvent(event)

    def update_path(self):
        if not self.start_point.isNull() and not self.end_point.isNull():
            self.graphics_line.setRect(QtCore.QRectF(self.start_point, self.end_point))
            self.graphics_line.setPen(QPen(
                QColor(242, 219, 7),
                3,
                QtCore.Qt.SolidLine,
                QtCore.Qt.FlatCap,
                QtCore.Qt.RoundJoin,
            ))

但是当右键单击以显示上下文菜单时,不可见的矩形会显示。

英文:

I have a class for draw rectangle and it works but when dragging rect and start_point(QPoint) is bigger than end_point, current rect won't show.

this clip show what is my problem!
在QGraphicScene中使用鼠标事件绘制矩形时出现问题。
and the code:

class RectangleScene(QGraphicsScene):
    def __init__(self, *args, **kwargs):
        super(RectangleScene, self).__init__(*args, **kwargs)
        self.graphics_line = None
        self.start_point = QtCore.QPointF()
        self.end_point = QtCore.QPointF()

    def clean_scene(self):
        for index, item in enumerate(self.items()[:-1]):
            if index > 1:


    def mousePressEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.clean_scene()
            self.start_point = event.scenePos()
            self.end_point = self.start_point
            self.graphics_line = QtWidgets.QGraphicsRectItem(QtCore.QRectF(self.start_point, self.end_point))
            self.addItem(self.graphics_line)
            self.update_path()

        elif QtCore.Qt.RightButton and self.graphics_line:
            self.contextMenuEvent(event)
            super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
            super(RectangleScene, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
            super(RectangleScene, self).mouseReleaseEvent(event)

    def update_path(self):
        if not self.start_point.isNull() and not self.end_point.isNull():
            self.graphics_line.setRect(QtCore.QRectF(self.start_point, self.end_point))
            self.graphics_line.setPen(QPen(
                QColor(242, 219, 7),
                3,
                QtCore.Qt.SolidLine,
                QtCore.Qt.FlatCap,
                QtCore.Qt.RoundJoin,
            ))

but when right click for context menu invisible rect showed.

答案1

得分: 1

你需要规范化你的 QRectF

class RectangleScene(QGraphicsScene):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.graphics_line = None
        self.start_point = QPointF()
        self.end_point = QPointF()

    def clean_scene(self):
        for index, item in enumerate(self.items()[:-1]):
            self.removeItem(item)

    def mousePressEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.clean_scene()
            self.start_point = event.scenePos()
            self.end_point = self.start_point
            self.graphics_line = QGraphicsRectItem(
                QRectF(self.start_point, self.end_point).normalized()  # 这里
            )
            self.addItem(self.graphics_line)
            self.update_path()
        elif Qt.RightButton and self.graphics_line:
            self.contextMenuEvent(event)
            super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
        super().mouseReleaseEvent(event)

    def update_path(self):
        if not self.start_point.isNull() and not self.end_point.isNull():
            self.graphics_line.setRect(
                QRectF(self.start_point, self.end_point).normalized()  # 这里
            )
            self.graphics_line.setPen(QPen(
                QColor(242, 219, 7), 3, Qt.SolidLine,
                Qt.FlatCap, Qt.RoundJoin,))
英文:

You need to normalize your QRectF:

class RectangleScene(QGraphicsScene):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.graphics_line = None
        self.start_point = QPointF()
        self.end_point = QPointF()

    def clean_scene(self):
        for index, item in enumerate(self.items()[:-1]):
            self.removeItem(item)

    def mousePressEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.clean_scene()
            self.start_point = event.scenePos()
            self.end_point = self.start_point
            self.graphics_line = QGraphicsRectItem(
                QRectF(self.start_point, self.end_point).normalized()  # here
            )
            self.addItem(self.graphics_line)
            self.update_path()
        elif Qt.RightButton and self.graphics_line:
            self.contextMenuEvent(event)
            super().mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
        super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            self.end_point = event.scenePos()
            self.update_path()
        super().mouseReleaseEvent(event)

    def update_path(self):
        if not self.start_point.isNull() and not self.end_point.isNull():
            self.graphics_line.setRect(
                QRectF(self.start_point, self.end_point).normalized()  # here
            )
            self.graphics_line.setPen(QPen(
                QColor(242, 219, 7), 3, Qt.SolidLine,
                Qt.FlatCap, Qt.RoundJoin,))

huangapple
  • 本文由 发表于 2023年2月16日 13:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定