Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Updating QGraphicsScene



  • This post is deleted!


  • This post is deleted!

  • Banned

    Okay I played around with your MRE doing a few things to analyze the situation and it appears that the documentation is perhaps incorrect (as it sometimes is for somethings) for I too could not get the update to work under any circumstance other than calling it specifically without any parameters. Although as a suggestion you might want to see how setSelectionArea and the clearSelection work as these might allow you to change specific zones within a Scene -- assuming you are needing that. Here is the MUC with my adjustments and extra things I tried -- but it appears .update() works like the .clear() in that it affects the entire scene as a whole and passing any coordinates to .update() causes it to not do anything

    from PyQt5.QtCore    import QRectF, QCoreApplication
    from PyQt5.QtGui     import QMouseEvent
    from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView
    
    # Always declare all other imports after your Qt Imports as
    # this prevents the miss association issue that sometimes 
    # occurs with Qt
    import sys
    
    class MainWindow(QMainWindow):
        def __init__(self):
          # You should not use super( ) in Python as it introduces 4 known major issues 
          # that must be handled properly. Further there were still actual bugs within
          # the usage of super( ) when used in Python. Yes while super( ) works fine 
          # within C++ it does not work as seamlessly within Python due to the major 
          # differences between these 2 languages. Next the reason it was created was 
          # to handle a rather rare issue and unless you are doing some complicated 
          # inheritance you will most likely never run into this extremely rare issue
          # However the 4 major issues that get included by using super( ) are much 
          # more likely to occur than that rare issue its meant to solve. Of course
          # using the basic explicit method, as follows, does not cause these issues and 
          # is as just as simple as using `super( )` further you do not actually gain 
          # anything useful by using `super( )` in Python that could not be done in a 
          # much safer manner.
          # super().__init__()
            QMainWindow.__init__(self)
    
            self.SetCnt = 0
    
            self.scene = QGraphicsScene()
            self.scene.setSceneRect(0,0,720,720)
    
            self.view = QGraphicsView()
            self.view.setViewportUpdateMode(QGraphicsView.NoViewportUpdate)
    #        self.view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
            self.view.setScene(self.scene)
    
            self.setCentralWidget(self.view)
    
        def mousePressEvent(self, MousEvnt):
            if self.SetCnt > 1:
                self.SetCnt = 0
                self.ResetThis = False
                print('Clearing Scene')
                self.scene.clear()
                self.scene.update()
            elif self.SetCnt == 0:
                print('Adding Line 1')
                self.SetCnt += 1
                self.scene.addLine(0, 0, 360, 360)
    #            self.scene.addLine(0, 0, 720,720)
    #            self.view.updateSceneRect(QRectF(0, 0, 360, 360))   # <-- This does not work
    #            self.scene.update(QRectF(0, 0, 360, 360))           # <-- This does not work
    #            QCoreApplication.processEvents()                    # <-- This has no affect
                self.scene.update()
            elif self.SetCnt == 1:
                print('Adding Line 2')
                self.SetCnt += 1
                self.scene.addLine(360, 360, 0,720)
                self.scene.update()
    
    if __name__ == "__main__":
      # If you are not going to handle Command Line arguments then do not include sys.argv
      # However if you do plan to use Command Line arguments then look into the argparser
      # library as this handles them much more intuitively, concisely, and efficiently no sense
      # reinventing a quality wheel one you can simply employ the one that already exists 
        MainEvntThred = QApplication([])
    
        MainApplication = MainWindow()
        MainApplication.show()
    
      # This is PyQt4 version of this line not PyQt5 although PySide2 still has to use this
      #  sys.exit(app.exec())
        MainEvntThred.exec()
    

    Note I did find this explanation : QGraphicsScene.update() is almost always a catchall for buggy code. If you need to use it, you are doing something wrong in 99%+ of cases. When you modify an item in the Scene, the item will notify the Scene of the change, the Scene will notify any Views, and the Views will redraw the scene (except when you specifically not to). Nothing extra needs to be done for this to happen. If you do something "to" an item and it is not immediately visible, you are not doing what you think you are doing: aka the item is not in the Scene, or is hidden, or you are not modifying the item at all.



  • This post is deleted!

  • Banned

    Yeah like I said I played with it for a while to see if I could get it to adhere to the documentation but to no avail so I am guessing that documentation is out of sync with what actually is and/or they broke it with an update such that it no longer does what it is supposed to do which also happens. Keep in mind we are playing with free software and for free it is mighty dang nice compared to some of that other free stuff out there.

    Btw did you try playing with the Layers and Items aspects -- I mean all the Scene does is keep track of the items and nothing more and its the View that actually handles the imaging -- which might be the reason the Scene.update() does not work with coordinates which is part of the reason I suggested looking into the setSelectionArea -- as it causes everything in a selected area to have focus and if that works then you might have be able to figure out how to get a handle on what you are trying to do.

    Basically what I was trying to do was draw 2 lines in the area and then delete (clear) just one of them -- so basically what I do is play around with the various methods available until I can figure out how to get it to do what I want it to do since the way it ought to work is not working as it is explained that it should.



  • This post is deleted!

  • Banned

    Yes but from my understanding of documentation of the Scene what you are "adding" to the pixmap are objects that it keeps track of and if you can figure out how that part works then you might be able to extrapolate that into an understanding of how to update the specific object within the View and thus what you are sort-of striving for -- keep in mind that things are already not acting as they are documented to -- so that means figuring out how they actual do work and how to connect your proverbial dots to get to where you want to be. If you discount it before exploring it you may be just closing the door on your actual solution.



  • This post is deleted!


  • This post is deleted!

Log in to reply