qt画roi矩形区域,周围八个点可伸缩可选择可增加删除
时间: 2023-03-20 13:01:09 浏览: 270
QT实现的具有放大图像功能的小窗口
如果你正在使用Qt来实现ROI矩形区域,那么可以考虑使用Qt中的QGraphicsView和QGraphicsScene来创建一个可视化的图形场景。以下是一些实现ROI矩形区域和周围八个点可伸缩、可选择、可增加和删除的示例代码:
```python
from PyQt5.QtCore import Qt, QRectF, QPointF
from PyQt5.QtGui import QBrush, QColor, QPen
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsRectItem, QGraphicsEllipseItem, QApplication
class ResizableRectItem(QGraphicsRectItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable)
# 创建周围八个点的图形项
self.topLeftHandle = QGraphicsEllipseItem(-5, -5, 10, 10, self)
self.topRightHandle = QGraphicsEllipseItem(self.rect().width() - 5, -5, 10, 10, self)
self.bottomLeftHandle = QGraphicsEllipseItem(-5, self.rect().height() - 5, 10, 10, self)
self.bottomRightHandle = QGraphicsEllipseItem(self.rect().width() - 5, self.rect().height() - 5, 10, 10, self)
# 设置周围八个点的属性
for handle in [self.topLeftHandle, self.topRightHandle, self.bottomLeftHandle, self.bottomRightHandle]:
handle.setBrush(QBrush(QColor(255, 255, 255)))
handle.setPen(QPen(QColor(0, 0, 0)))
# 设置周围八个点的标志
self.topLeftHandle.setFlags(QGraphicsItem.ItemIsMovable)
self.topRightHandle.setFlags(QGraphicsItem.ItemIsMovable)
self.bottomLeftHandle.setFlags(QGraphicsItem.ItemIsMovable)
self.bottomRightHandle.setFlags(QGraphicsItem.ItemIsMovable)
def itemChange(self, change, value):
# 当ROI矩形项移动或大小更改时,更新周围八个点的位置
if change == QGraphicsItem.ItemPositionChange:
self.topLeftHandle.setPos(self.rect().topLeft() + QPointF(5, 5))
self.topRightHandle.setPos(self.rect().topRight() + QPointF(-5, 5))
self.bottomLeftHandle.setPos(self.rect().bottomLeft() + QPointF(5, -5))
self.bottomRightHandle.setPos(self.rect().bottomRight() + QPointF(-5, -5))
elif change == QGraphicsItem.ItemRectChange:
self.topLeftHandle.setPos(self.rect().topLeft() + QPointF(5, 5))
self.topRightHandle.setPos(self.rect().topRight() + QPointF(-5, 5))
self.bottomLeftHandle.setPos(self.rect().bottomLeft() + QPointF(5, -5))
self.bottomRightHandle.setPos(self.rect().bottomRight() + QPointF(-5, -5))
return super().itemChange(change, value)
class ROIScene(QGraphicsScene):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setSceneRect(0, 0, 800, 600)
# 创建一个ROI矩形项
self.rectItem =
阅读全文