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.
...............
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)
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))
painter.setPen(QPen(Qt.lightGray))
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:
.......
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()
class MainWindow(QMainWindow):
.......
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()
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?
Bookmarks