python - 在 PyQt 中调整窗口大小后如何继续在图像上绘图?
问题描述
我已经编写了一个代码来在QGraphicsView中的图像上绘制一个矩形,但是如果我调整图像的大小,例如全屏,矩形的位置就会错位。有没有什么办法解决这一问题?我认为一种可能的解决方案是每次在左上角对齐图像,但我不能在setAlignment()中给出 2 个参数。
这是代码:(按钮仅用于显示atm)
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.b1 = QPushButton('Next Image')
self.b2 = QPushButton('Crop')
self.b3 = QPushButton('Clear')
self.view = GraphicsView()
h_box = QHBoxLayout()
v_box = QVBoxLayout()
v_box.addWidget(self.b1)
v_box.addWidget(self.b2)
v_box.addWidget(self.b3)
h_box.addWidget(self.view)
h_box.addLayout(v_box)
self.setLayout(h_box)
#self.resize(800, 800)
self.setWindowTitle("Super Duper Cropper")
self.show()
class GraphicsView(QGraphicsView):
def __init__(self):
super().__init__()
self.setScene(QGraphicsScene())
self.item = QGraphicsPixmapItem(QPixmap('test.jpg'))
self.scene().addItem(self.item)
def mousePressEvent(self, event):
self.xi = event.x()
self.yi = event.y()
def mouseMoveEvent(self, event):
self.xf = event.x()
self.yf = event.y()
self.draw_rect()
def mouseReleaseEvent(self, event):
self.xf = event.x()
self.yf = event.y()
self.draw_rect()
def draw_rect(self):
self.scene().removeItem(self.item)
self.scene().addItem(self.item)
self.scene().addRect(self.xi, self.yi, self.xf-self.xi, self.yf-self.yi, pen=QPen(QColor(51, 153, 255), 2,
Qt.SolidLine), brush=QBrush(QColor(0, 255, 0, 40)))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWidget()
window.show()
app.aboutToQuit.connect(app.deleteLater)
sys.exit(app.exec_())
解决方案
您有以下错误:
场景的坐标和视图的坐标不同,所以
mapToScene()
要建立矩形的正确位置就必须使用该方法。为什么要添加和删除项目?最好的办法是重用
您希望矩形的位置相对于
QGraphicsPixmapItem
,因此矩形必须是 的子级QGraphicsPixmapItem
。
使用上面我们得到以下信息:
class GraphicsView(QGraphicsView):
def __init__(self):
super().__init__()
self.setScene(QGraphicsScene())
self.item = QGraphicsPixmapItem(QPixmap('test.jpg'))
self.scene().addItem(self.item)
self.rect_item = QGraphicsRectItem(QRectF(), self.item)
self.rect_item.setPen(QPen(QColor(51, 153, 255), 2, Qt.SolidLine))
self.rect_item.setBrush(QBrush(QColor(0, 255, 0, 40)))
def mousePressEvent(self, event):
self.pi = self.mapToScene(event.pos())
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
pf = self.mapToScene(event.pos())
if (self.pi - pf).manhattanLength() > QApplication.startDragDistance():
self.pf = pf
self.draw_rect()
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
pf = self.mapToScene(event.pos())
if (self.pi - pf).manhattanLength() > QApplication.startDragDistance():
self.pf = pf
self.draw_rect()
super().mouseReleaseEvent(event)
def draw_rect(self):
r = QRectF(self.pi, self.pf).normalized()
r = self.rect_item.mapFromScene(r).boundingRect()
self.rect_item.setRect(r)
推荐阅读
- python - 使用 PIL 将 512*256 的图像分成 2 个 256*256 的图像
- python - 如何融化 0 和 1 的数据框并只保留 1
- node.js - 保持活动仅与出站请求有关吗?
- google-sheets - 如何在 ArrayFormula 中使用 COUNTUNIQUE 函数并获得范围结果?
- excel - 从唯一过滤中获取重复结果
- sql - 具有未知列数的简单 SQL Pivot 查询
- android-studio - 取消选中使用 Androidx 工件
- python - 这行“return self.last_block['index'] + 1”是如何工作的?
- excel - VBA从一个单元格逐字匹配另一个单元格中的逐字
- html - 您应该使用什么标签来制作自定义标题而不是