May-03-2021, 02:30 PM
I have the following code that adds a couple of features to QWidget and QGroupBox.
In C++ I would make a template. How would you do this in Python?
class View(QtWidgets.QWidget): """Add upload and update loop to QWidget""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.update_list = UpdateList(1.0) 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() 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 GroupView(QtWidgets.QGroupBox): """Add upload and update loop to QBoxGroup""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.update_list = UpdateList(1.0) 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() 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()The code is identical except for the superclass. I would like to make a superclass that implements the common behavior, but openEvent(), closeEvent() and self.children() are all part of the new behavior and these methods are inherited from QWidget.
In C++ I would make a template. How would you do this in Python?