如何确定QGraphicsPixmapItem的可见坐标?

2024-07-08 09:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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。当用户调整显示图像的窗口的大小时,问题就出现了。我没有在windows resize事件中调用“fitInView(…)”,因为我更喜欢默认的行为,即缩放保持不变,但当用户调整窗口大小时,图像被裁剪(或更多图像进入视图)。我的问题是,当用户调整窗口大小以便于存储时,如何确定图像(pixmap)的可视部分(原因是我正在尝试更新一个感兴趣的区域矩形,该矩形绘制在较小的图像表示之上,并且需要在其纵横比方面保持同步)当用户调整包含图像的窗口的大小时定位。)我四处搜寻,但找不到任何东西。非常感谢您的帮助

更新:我刚刚意识到我可能可以在resize事件中检查窗口尺寸,然后映射到场景。以下的一些变体:

def resizeEvent(self, event):

    newWidth = self.width()
    newHeight = self.height()
    uperLeftROICoord = self.graphicsView.mapToScene(0, 0)
    lowerRightROICoord = self.graphicsView.mapToScene(newWidth - 1, newHeight - 1)

Tags: 用户图像selfinitdef方式sceneparent

热门问题