Using Qt 4.7 with PyQt.

I was under the impression that QGraphicsView would adjust to any scene size changes. In particular it would add scroll bars after a scene size change that makes the scene larger than the current viewport.

However, this does not seem to be the case, for example,
Qt Code:
  1. class Window(QWidget):
  2. def __init__(self, parent=None):
  3. QWidget.__init__(self, parent)
  4. initialSceneRect = QRectF(-10, -10, 20, 20)
  5. self.scene = QGraphicsScene(initialSceneRect)
  6. # Widgets
  7. self.view = QGraphicsView(self.scene, self)
  8. # Layout
  9. layout = QHBoxLayout(self)
  10. layout.addWidget(self.view)
  11. # Add items to scene
  12. it = QGraphicsRectItem(QRectF(-100, -100, 200, 200))
  13. it.setPos(QPointF(0, 0))
  14. self.scene.addItem(it)
  16. #def sizeHint(self):
  17. # return QSize(600,600)
  19. def keyPressEvent(self, evt):
  20. """ quick and dirty way to add an object on the fly"""
  21. it = QGraphicsRectItem(QRectF(-1000, -1000, 2000, 2000))
  22. it.setPos(QPointF(0, 0))
  23. self.scene.addItem(it)
  24. self.view.updateSceneRect(QRectF(-1100, -1100, 2200, 2200))
  27. def main():
  28. app = QApplication([])
  29. w = Window()
  31. app.exec_()
  33. main()
To copy to clipboard, switch view to plain text mode 

Note that reimplementation of sizeHint() is commented out.

In the case sizeHint() is commented out, this program puts up a window that is not even large enough to see the first QGraphicsRectItem. It does not have scrollbars. Upon adding the second rect item (via pressing any key) nothing changes.

In the case sizeHint() is used, this program puts up a 600x600 which means it is large enough to see the first rect item. When resizing it to be small, it does NOT add scrollbars.

Note that the scene is initialized with a small sceneRect. When this is made larger, then at least the view adds scrollbars when you make it small.

So it appears that the view's concept of the sceneRect is simply the initial scene rect. It never changes even after adding other graphic items. Note that I'm calling QGraphicsView.updateSceneRect() in keyPressEvent() but it does nothing apparently!