我有一个基于PyQt QGraphicsView
和QGraphicsScene
的查看模型。
我在场景中添加了两个项目。
那部分运作良好。
但是,当我选择缩放视图时,使用scale(factor,factor)
api,左侧项目占用的区域比右侧项目多得多。
我希望能够对称地显示两个项目的比例效果。
或者,我希望有两个QGraphicsView
实例同时收听wheelEvent
(为了集中注意力)和RubberBandDrag
函数的事件,以便拥有在所有QGraphicsView
个实例上同时进行平移。
这是我正在使用的代码:
class PhotoViewer(QtGui.QGraphicsView):
photoClicked = QtCore.pyqtSignal(QtCore.QPoint)
def __init__(self, parent,theLayout):
super(PhotoViewer, self).__init__(parent)
self._zoom = 0
self.layout = theLayout
self._empty = True
self.parent = parent
self._scene = QtGui.QGraphicsScene(self)
self._photo = QtGui.QGraphicsPixmapItem()
self._photo1 = QtGui.QGraphicsPixmapItem()
self._scene.addItem(self._photo)
self._scene.addItem(self._photo1)
self.setScene(self._scene)
self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
self.setFrameShape(QtGui.QFrame.NoFrame)
def hasPhoto(self):
return not self._empty
def setPhoto(self, pixmap=None):
self._zoom = 0
width = self.parent.width()
height= self.height()
self._scene.setSceneRect(QRectF(0.0, 0.0, width + 20.0, height))
if pixmap and not pixmap.isNull():
self._empty = False
self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
pixmap =pixmap.scaled(width/2,height)
self._photo.setPixmap(pixmap)
image = QPixmap("IMG_1612_02_1120_1542_bp.bmp")
image = image.scaled(width/2, height)
self._photo1.setPixmap(image)
self._photo.setOffset(0,0.0)
self._photo1.setOffset(width / 2 + 20, 0.0)
self.scale(1.5, 1.5)