0

The overlay is complex so I exploited "PyQt5.uic". Note that "graphicsViewObj" object is instantiated already and it is loaded from a ui file. Now I need to overwrite some method in QGraphicsView (For example. a "wheelEvent" method). A new class is inherited from a QGraphicsView to overwrite wheelEvent method. Now the problem came out, How do I connect the existing object(loaded with "uic") to my overwrited class(new QGraphicsView class)? The following code elaborate my idea:

class myQGraphicsView(QGraphicsView):

    def __init__(self,container):
        super(myQGraphicsView,self).__init__()

    def wheelEvent(self,event):
        super().wheelEvent(event)
        # my change here!!!

class Window(QMainWindow):

    def __init__(self):
        super().__init__()
        uic.loadUi('window.ui',self)
        self.initialize()
        self.bindings()
        self.myQGraphicsView = myQGraphicsView() #instantiate myQGraphicsView
        self.myQGraphicsView = self.graphicsViewObj # I made this up to explain what I wanted to do
        # the last line was a mistake I need to copy everything from 
        # 'self.graphicsViewObj' to 'self.myQGraphicsView' but with new 
        # overwritten method 'eventWheel'

forgive me for some flaws in my code but u get the idea.

  • *Promote* the graphics view in Designer using your class name and script name (without extension) containing the class as the "header file". Remember that if you want to use the same file for both your subclass *and* the main program, you **must** use the `if __name__ == '__main__':` block to create the application and everything else. Also, classes (and constants) should always have capitalized names. – musicamante Oct 21 '22 at 14:22
  • Thank you for prompt me this method. It perfectly solved by 'promote to' – vodemortMercy Oct 24 '22 at 01:42

0 Answers0