我正在使用 PyQt 通过以下方式显示图像(我省略了一些繁琐的细节只是为了展示一般方法):
class MyGraphicsView(QGraphicsView):
def __init__(self, parent = None):
super(MyGraphicsView, self).__init__(parent)
self.scene = QGraphicsScene()
qImage = QImage(myPixels.data, numColumns, numRows, QImage.Format_Indexed8)
qImage.setColorTable(self.gray_color_table)
self.sceneImage = self.scene.addPixmap(QPixmap.fromImage(qImage))
一切正常,图像正确显示在我的窗口中。然后我通过执行以下操作来显示图像的一部分:
self.fitInView(roiCoords[0], roiCoords[2], roiCoords[1]-roiCoords[0], roiCoords[3]-roiCoords[2], Qt.KeepAspectRatio)
其中“roiCoords”只是一个包含在运行时以编程方式生成的 xmin、xmax、ymin、ymax 的列表。当用户调整显示图像的窗口大小时,就会出现问题。我没有在窗口调整大小事件中调用“fitInView(...)”,因为我更喜欢默认行为,即缩放保持不变,但随着用户调整窗口大小,图像被裁剪(或更多图像进入视图)。我的问题是如何确定当用户调整窗口大小以便可以存储它们时可见的图像部分(像素图)(原因是我正在尝试更新感兴趣的矩形区域)s 被绘制在图像的较小表示之上,并且当用户调整包含图像的窗口大小时,需要在其纵横比和位置方面保持同步。)我四处寻找,但找不到任何东西. 非常感谢任何帮助。
更新:我刚刚意识到我可以在调整大小事件中检查窗口尺寸,然后映射到场景。以下的一些变体:
def resizeEvent(self, event):
newWidth = self.width()
newHeight = self.height()
uperLeftROICoord = self.graphicsView.mapToScene(0, 0)
lowerRightROICoord = self.graphicsView.mapToScene(newWidth - 1, newHeight - 1)