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
You can create a class that implements the common functionality and then the buttons inherit from the item and the common class: