Is it possible to update a QGraphicsScene Object only in a specific area?

Thoms Müller ipsoka96 at
Fri May 8 19:40:37 BST 2020

I've been struggling with this for a while now.
I have a QGraphicsView and in it a QGraphicsScene. At runtime I am trying to
update only specific areas in the QGraphicsScene but it doesnt quiet work.
Here is some minimal code showing the problem:

import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc

class MainWindow(qtw.QMainWindow):
    def __init__(self):
        self.scene = qtw.QGraphicsScene()
        self.view = qtw.QGraphicsView()

    def mousePressEvent(self, a0: qtg.QMouseEvent) -> None:
        self.scene.addLine(0, 0, 720, 720)
        self.scene.update(0, 0, 720, 720)     #<--- This does not work but
should work
        #self.scene.update()                <--- This works for some reason

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    mainWindow = MainWindow()

For some reason, calling update() without parameter on the scene object
works and the added line shows up at runtime when clicking, but when i
specify the area to be updated by passing in x, y, width and height it
doesnt work. Any ideas?

Note: I set the views viewportupdatemode to NoViewportUpdate because the
docs state the following:
QGraphicsView will never update its viewport when the scene changes; the
user is expected to control all updates. This mode disables all (potentially
slow) item visibility testing in QGraphicsView, and is suitable for scenes
that either require a fixed frame rate, or where the viewport is otherwise
updated externally.

This is what I want, since i only want to update specific areas almost every
frame, not the whole scene over and over again. When is set the Viewportmode
to something else, it seems like update() calls on the QgraphicsScene object
within the QGraphicsView are being ignored.
I would really appreciate any help

Sent from:

More information about the PyQt mailing list