pythonpyside2qgraphicssceneqgraphicsrectitemqgraphicsellipseitem

How can I subclass QGraphicsRectItem and QGraphicsEllipseItem in one class?


I'm using Pyside2 and have a UI that uses QGraphicsView and a QGraphicsScene.

Right now I have two separate classes that subclass QGraphicsEllipseItem and QGraphicsRectItem like this:

class MyRectButton(QGraphicsRectItem):

    def contextMenuEvent(self, event):
        # custom context menu
        pass

    def custom_method_A(self):
        # add a custom method
        pass

    def custom_method_B(self):
        # add a custom method
        pass

class MyEllipseButton(QGraphicsEllipseItem):

    def contextMenuEvent(self, event):
        # custom context menu
        pass

    def custom_method_A(self):
        # add a custom method
        pass

    def custom_method_B(self):
        # add a custom method
        pass

Rather than have the redundant methods in both classes I'd like one class that can either be a rectangle or an ellipse like:

class MyButton():
    def __init__(self,shape='Ellipse'):
        pass

    def contextMenuEvent(self, event):
        # custom context menu
        pass

    def custom_method_A(self):
        # add a custom method
        pass

    def custom_method_B(self):
        # add a custom method
        pass



Solution

  • You can create a class that implements the common functionality and then the buttons inherit from the item and the common class:

    from PyQt5 import QtCore, QtWidgets
    
    
    class Foo:
        def contextMenuEvent(self, event):
            print("contextMenuEvent")
    
        def custom_method_A(self):
            print("custom_method_A")
    
        def custom_method_B(self):
            print("custom_method_B")
    
    
    class MyRectButton(QtWidgets.QGraphicsRectItem, Foo):
        pass
    
    
    class MyEllipseButton(QtWidgets.QGraphicsEllipseItem, Foo):
        pass
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        scene = QtWidgets.QGraphicsScene()
        view = QtWidgets.QGraphicsView(scene)
    
        it_rect = MyRectButton(QtCore.QRectF(-50, -30, 100, 100))
        it_ellipse = MyEllipseButton(QtCore.QRectF(50, 50, 100, 100))
    
        scene.addItem(it_rect)
        scene.addItem(it_ellipse)
    
        for it in (it_rect, it_ellipse):
            it.custom_method_A()
            it.custom_method_B()
    
        view.resize(640, 480)
        view.show()
        sys.exit(app.exec_())