|
我有以下代码,为QWidget和QGroupBox添加了几个功能。
- 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()
复制代码 代码是相同的。我想制作一个实现常见行为的超类,但是openEvent(),closeEvent()和self.children()都是新行为的一部分,并且这些方法是从QWidget继承的。
在C ++中,我将创建一个模板。您将如何在Python中做到这一点?
本帖寻求最佳方案
回复被采纳后将获得奖励 C币 5
,目前已有 1 个回复
我要奖励
|
|