May-03-2021, 08:32 PM
I tried a mixin. It works, but the mixin provides only half the additional functionality because it really needs to act like a QWidget to get the show/close events.
class BaseView(): """Abstract base class for views""" def __init__(self): self.update_list = UpdateList(1.0) def upload(self): """Forward update message to all ValueControl objects""" if self.isVisible(): for child in self.children(): if isinstance(child, ValueControl): child.update_value() class View(QtWidgets.QWidget, BaseView): """Add upload and update loop to QWidget""" def __init__(self, *args, **kwargs): QtWidgets.QWidget.__init__(self, *args, **kwargs) BaseView.__init__(self) def showEvent(self, event): """Start update loop when window is shown""" self.upload() self.update_list.start() event.accept() def closeEvent(self, event): """Stop update loop when window is closed""" self.update_list.stop() event.accept()A benefit of this approach is giving View and GroupView a common superclass makes them easier to find using methods like QtWidget.findChildren(BaseView).