QGraphicsView.fitInView的令人困惑的行为



我正在尝试制作一个具有两个QGraphicsView的小部件,以并排比较两个图像。当用户放大其中一个视图时,另一个视图应放大到同一区域。我的zoomIn函数会发送一个带有可见场景矩形的信号,而另一个视图中的一个插槽会捕获该信号并调用QGraphicsView.fitInView(rectangle)以使同一图像区域可见。令我惊讶的是,fitInView似乎将矩形(x,y,width,height(视为(x,y,x+width,y+height(。这是bug还是我遗漏了什么?

这是我的代码示例:

QGraphicsView子类

import logging
from PyQt5 import (
QtWidgets as qw,
QtCore as qc,
QtGui as qg
)
class Display(qw.QGraphicsView):
sigViewportAreaChanged = qc.pyqtSignal(qc.QRectF)
def __init__(self, *args, **kwargs):
super(Display, self).__init__(*args, **kwargs)
...
self.zoomInAction = qw.QAction('Zoom in')
self.zoomInAction.triggered.connect(self.zoomIn)
@qc.pyqtSlot()
def zoomIn(self):
self.scale(1.2, 1.2)
pos = self.viewport().pos()
size = self.viewport().size()
logging.debug(f'{self.objectName()}: viewport:: pos: {pos}, size: {size}')
pos = self.mapToScene(pos)
size = self.mapToScene(size.width(), size.height())
logging.debug(f'{self.objectName()}: scene:: pos: {pos}, size: {size}')
view_area = qc.QRectF(pos.x(), pos.y(), size.x(), size.y())
logging.debug(
f'Emitting changed viewport area, {view_area},n{mat}')
self.sigViewportAreaChanged.emit(view_area)
def setViewportRect(self, rect: qc.QRectF) -> None:
logging.debug(f'{self.objectName()} <- {self.sender().objectName()}nFit view to {rect}')
pos = self.viewport().pos()
size = self.viewport().size()
logging.debug(f'{self.objectName()}: before fitting area: viewport:: pos: {pos}, size: {size}')
pos = self.mapToScene(pos)
size = self.mapToScene(size.width(), size.height())
logging.debug(f'{self.objectName()}: before fitting area: scene:: pos: {pos}, size: {size}')
# self.fitInView(rect)  # Incorrectly sets the visible area
self.fitInView(rect.x(), rect.y(), rect.width() - rect.x(), rect.height() - rect.y())  # this works
pos = self.viewport().pos()
size = self.viewport().size()
logging.debug(f'{self.objectName()}: after fitting area: viewport:: pos: {pos}, size: {size}')
pos = self.mapToScene(pos)
size = self.mapToScene(size.width(), size.height())
logging.debug(f'{self.objectName()}: after fitting area: scene:: pos: {pos}, size: {size}')  # this suggests that the new viewport is (x, y, x+width, y+height) instead of (x, y, width, height)

容器小部件:

class ReviewWidget(qw.QWidget):
"""A widget with two panes for comparing images"""
def __init__(self, *args, **kwargs):
super(ReviewWidget, self).__init__(*args, **kwargs)
layout = qw.QVBoxLayout()
self.before = Display()
self.before.setObjectName('Left')
self.after = Display()
self.after.setObjectName('Right')
self.before.setHorizontalScrollBarPolicy(qc.Qt.ScrollBarAlwaysOn)
self.before.setVerticalScrollBarPolicy(qc.Qt.ScrollBarAlwaysOn)
self.after.setHorizontalScrollBarPolicy(qc.Qt.ScrollBarAlwaysOn)
self.after.setVerticalScrollBarPolicy(qc.Qt.ScrollBarAlwaysOn)
panes_layout = qw.QHBoxLayout()
panes_layout.addWidget(self.before)
panes_layout.addWidget(self.after)
layout.addLayout(panes_layout)
self.setLayout(panes_layout)
self.make_actions()
def tieViews(self, tie):
if tie:
self.before.sigViewportAreaChanged.connect(self.after.setViewportRect)
self.after.sigViewportAreaChanged.connect(self.before.setViewportRect)
else:
self.before.disconnect(self.before.sigViewportAreaChanged)
self.after.disconnect(self.after.sigViewportAreaChanged)
def make_actions(self):
self.tieViewsAction = qw.QAction('Zoom views together')
self.tieViewsAction.setCheckable(True)
self.tieViewsAction.triggered.connect(self.tieViews)

我认为您计算size的方式是错误的。您应该根据pos的翻译值来计算它。由于您知道宽度和高度的值,因此可以根据pos的场景值计算view_area的右下角。

viewport_pos = self.viewport().pos()
scene_pos = self.mapToScene(viewport_pos)
width = self.viewport().size().width()
height = self.viewport().size().height()
lower_right_corner = QPointF(width - scene_pos.x(), height() - scene_pos.y())

如果不做减法,则假设view_area的原点在(0, 0),而实际上它是pos

好吧,我在阅读@Roney Gomes的答案后发现了我的错误。

为了将大小缩放到场景坐标系,我将大小作为点传递到mapToScene

但点和大小在概念上是不同的,虽然大小不取决于坐标系的原点,但点是。mapToScene将其论点视为一个点,对其进行平移以反映新的坐标系。

因此,我需要减去新坐标系的原点,它实际上是视口的左上角,以返回大小。

一个更简单的解决方案是直接映射视口矩形:

rect = self.mapToScene(self.viewport().rect())  
rect = rect.boundingRect() # mapToScene(rect) returns QPolygonF
self.sigViewportAreaChanged.emit(rect)

相关内容

  • 没有找到相关文章

最新更新