英文:
Problem in drawing Rectangle with mouse event in QGraphicScene
问题
我有一个用于绘制矩形的类,它可以正常工作,但当拖动矩形并且起始点(QPoint)大于终止点时,当前矩形不会显示。
而这是代码:
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!
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,))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论