I have a QTableWidget and QListWidget that I want to subclass with similar override methods like mousePressEvent(). In an effort to keep my code dry, I would prefer not to have two unique subclasses that have duplicate logic. Can anyone suggest a pattern to use in this case?
Pseudo code:
from PySide2 import QtWidgets, QtCore
class ChildClass(QtWidgets.QListWidget): # Could be QTableWidget
_main_window = None
_context_menu = None
def __init__(self, main_window):
super().__init__(main_window)
self._main_window = main_window
self.setDragEnabled(True)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
def mousePressEvent(self, event):
super().mousePressEvent(event)
self._main_window.set_active_browser(self.parent())