PyQt: Trying to understand graphics scene/view

From the document

If the scene rect is unset, PySide.QtGui.QGraphicsScene will use the bounding area of all >items, as returned by PySide.QtGui.QGraphicsScene.itemsBoundingRect() , as the scene rect.

So it's setting the rect of your image as your scene rect and the center of the scene is the center of the widget.

The scroll bars will appear when the scene rect is larger than the widget size. When you comment out the setSceneRect line your scene is being resized automatically but when you have the line your images are being added past the bounds of the scenRect so you would have to update your sceneRect to show it.


import sys 
from PyQt4 import QtGui, QtCore

class MyView(QtGui.QGraphicsView):
    def __init__(self):
        QtGui.QGraphicsView.__init__(self)

        self.setGeometry(QtCore.QRect(100, 100, 600, 250))

        self.scene = QtGui.QGraphicsScene(self)
        self.scene.setSceneRect(QtCore.QRectF())

        self.setScene(self.scene)

        for i in range(5):
            self.item = QtGui.QGraphicsEllipseItem(i*75, 10, 60, 40)
            self.scene.addItem(self.item)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    view = MyView()
    view.show()
    sys.exit(app.exec_())