雖然嘗試實現項目大小不會改變但項目之間的距離得到放大的場景,但我用下面的代碼繪製了一個矩形和文本「A」。現在,如果在矩形項目上設置標誌ItemIgnoresTransformations,則放大會導致矩形消失(單擊並拖動矩形周圍的鼠標按鈕)。但是,在案文中不會發生這種情況。此外,由fitInView設置的新視區面積與我所要求的差異很大:當ItemIgnoresTransformations處於打開狀態時QGraphicsView的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_()
任何解釋將不勝感激!
謝謝,這適用於「消失」問題。但我認爲這種情況下的局部座標是場景座標。顯然情況並非如此。 – subhacom