Can't manually update QGraphicsScene
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.
Code:
...............
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
))
painter.
setPen(QPen(Qt.
lightGray)) painter.drawLines(lines)
I want to be able to toggle the grid by using the menu:
Code:
.......
def createActions(self):
displayGrid
= QAction('Display Grid', self
) displayGrid.setCheckable(True)
displayGrid.setChecked(self.scene.gridActive)
displayGrid.setShortcut('Ctrl+G')
displayGrid.setStatusTip('Display or Hide the grid')
self.connect(displayGrid, SIGNAL('toggled(bool)'), self.setDisplayGrid)
viewMenu = self.menubar().addMenu('&View')
viewMenu.addAction(displayGrid)
def setDisplayGrid(self, value):
self.scene.gridActive = value
self.scene.update()
(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?
Re: Can't manually update QGraphicsScene
We'd have to see the bigger picture. It is likely you are drawing outside boundingRects of your items or something like that.
Re: Can't manually update QGraphicsScene
Hmm,
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)
http://pastebin.com/m264f6728
Re: Can't manually update QGraphicsScene
It would be best to isolate the problem using a minimal compilable example.
Re: Can't manually update QGraphicsScene
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.