View Full Version : Can't manually update QGraphicsScene

16th January 2010, 19:28
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.

class Scene(QGraphicsScene):

def drawBackground(self, painter, rect):
if self.gridActive:
gridSize = 50
left = int(rect.left()) - (int(rect.left()) % gridSize)
top = int(rect.top()) - (int(rect.top()) % gridSize)
lines = []
right = int(rect.right())
bottom = int(rect.bottom())
for x in range(left, right, gridSize):
lines.append(QLineF(x, rect.top(), x, rect.bottom()))
for y in range(top, bottom, gridSize):
lines.append(QLineF(rect.left(), y, rect.right(),y))


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

class MainWindow(QMainWindow):

def createActions(self):
displayGrid = QAction('Display Grid', self)
displayGrid.setStatusTip('Display or Hide the grid')

self.connect(displayGrid, SIGNAL('toggled(bool)'), self.setDisplayGrid)
viewMenu = self.menubar().addMenu('&View')

def setDisplayGrid(self, value):
self.scene.gridActive = value

(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?

17th January 2010, 11:34
We'd have to see the bigger picture. It is likely you are drawing outside boundingRects of your items or something like that.

17th January 2010, 22:33

I tried self.view.fitInView(self.scene.itemsBoundingRect() ,Qt.KeepAspectRatio) but it also didn't work.

I don't know if you'd mind looking at the big picture but here is the complete code:
(unfortunately I'm using some other lib for data import. The Scene/View and MainWindow implementations are at the bottom. Top classes are not important)


17th January 2010, 22:39
It would be best to isolate the problem using a minimal compilable example.

17th January 2010, 22:52
Oopps, I'm a bit dizzy late at night. I've produced a minimal example and figured out that the problem is with the line:


Removing it solved the problem. I just tought it could speed up the performance of drawing grid.

Thank you for your help.