I'm working with PyQt4.5.4.

Summary: QGraphicsScene.update() method doesn't update the scene immediately.

Detailed Description:
I have a QGraphicsScene, with drawBackground method to draw background grids.

Qt Code:
  1. class Scene(QGraphicsScene):
  2. ...............
  3.  
  4. def drawBackground(self, painter, rect):
  5. if self.gridActive:
  6. gridSize = 50
  7. left = int(rect.left()) - (int(rect.left()) % gridSize)
  8. top = int(rect.top()) - (int(rect.top()) % gridSize)
  9. lines = []
  10. right = int(rect.right())
  11. bottom = int(rect.bottom())
  12. for x in range(left, right, gridSize):
  13. lines.append(QLineF(x, rect.top(), x, rect.bottom()))
  14. for y in range(top, bottom, gridSize):
  15. lines.append(QLineF(rect.left(), y, rect.right(),y))
  16.  
  17. painter.setPen(QPen(Qt.lightGray))
  18. painter.drawLines(lines)
To copy to clipboard, switch view to plain text mode 

I want to be able to toggle the grid by using the menu:

Qt Code:
  1. class MainWindow(QMainWindow):
  2. .......
  3.  
  4. def createActions(self):
  5. displayGrid = QAction('Display Grid', self)
  6. displayGrid.setCheckable(True)
  7. displayGrid.setChecked(self.scene.gridActive)
  8. displayGrid.setShortcut('Ctrl+G')
  9. displayGrid.setStatusTip('Display or Hide the grid')
  10.  
  11. self.connect(displayGrid, SIGNAL('toggled(bool)'), self.setDisplayGrid)
  12. viewMenu = self.menubar().addMenu('&View')
  13. viewMenu.addAction(displayGrid)
  14.  
  15. def setDisplayGrid(self, value):
  16. self.scene.gridActive = value
  17. self.scene.update()
To copy to clipboard, switch view to plain text mode 

(scene.gridActive is initially True)

But the self.scene.update() method doesn't update it immediately. But rather I have to zoom in/out or resize the window to make the toggle effect get work.

I used to be able to do it but somehow corrupted it and can't find the source of the problem. Any ideas?