在尝试实现项目大小不变但项目之间的距离被放大的场景时,我遇到了以下代码的问题,该代码绘制了一个矩形和文本“A”。现在,如果我在矩形项目上设置标志 ItemIgnoresTransformations,放大会导致矩形消失(单击并在矩形周围拖动鼠标按钮)。但在文本的情况下不会发生这种情况。此外,fitInView 设置的新视口区域与我要求的非常不同:
import sys
from PyQt4 import QtCore, QtGui
class GV(QtGui.QGraphicsView):
def __init__(self, *args, **kwargs):
QtGui.QGraphicsView.__init__(self, *args, **kwargs)
def mousePressEvent(self, event):
pos = QtCore.QPoint(event.pos())
self.startPos = pos
def mouseReleaseEvent(self, event):
pos = QtCore.QPoint(event.pos())
self.endPos = pos
rect = QtCore.QRect(self.startPos, self.endPos)
sceneRect = self.mapToScene(rect).boundingRect()
print 'Selected area: viewport coordinate:', rect, \
', scene coordinate:', sceneRect
self.fitInView(sceneRect)
print 'new viewport in scene coordinates:', \
self.mapToScene(self.viewport().geometry()).boundingRect()
class Scene(QtGui.QGraphicsScene):
def __init__(self, *args, **kwargs):
QtGui.QGraphicsScene.__init__(self, *args, **kwargs)
self.itemA = QtGui.QGraphicsSimpleTextItem('A')
self.itemA.setPos(20, 20)
self.itemA.setFlag(QtGui.QGraphicsItem.ItemIgnoresTransformations, True)
self.addItem(self.itemA)
self.itemB = QtGui.QGraphicsRectItem(30, 50, 20, 20)
self.addItem(self.itemB)
self.itemB.setFlag(QtGui.QGraphicsItem.ItemIgnoresTransformations, True)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
widget = QtGui.QMainWindow()
scene = Scene()
view = GV(scene, widget)
widget.setCentralWidget(view)
widget.show()
app.exec_()
任何解释将不胜感激!