小白教程

 找回密码
 立即注册
查看: 5728|回复: 1

[已回复] Python C ++模板

[复制链接]

176

主题

185

帖子

663

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
663
发表于 2021-5-18 00:28:10 | 显示全部楼层 |阅读模式
我有以下代码,为QWidget和QGroupBox添加了几个功能。
  1. class View(QtWidgets.QWidget):
  2.     """Add upload and update loop to QWidget"""

  3.     def __init__(self, *args, **kwargs):
  4.         super().__init__(*args, **kwargs)
  5.         self.update_list = UpdateList(1.0)

  6.     def showEvent(self, event):
  7.         """Start update loop when window is shown"""
  8.         self.upload()
  9.         self.update_list.start()
  10.         event.accept()

  11.     def closeEvent(self, event):
  12.         """Stop update loop when window is closed"""
  13.         self.update_list.stop()
  14.         event.accept()

  15.     def upload(self):
  16.         """Forward update message to all ValueControl objects"""
  17.         if self.isVisible():
  18.             for child in self.children():
  19.                 if isinstance(child, ValueControl):
  20.                     child.update_value()


  21. class GroupView(QtWidgets.QGroupBox):
  22.     """Add upload and update loop to QBoxGroup"""

  23.     def __init__(self, *args, **kwargs):
  24.         super().__init__(*args, **kwargs)
  25.         self.update_list = UpdateList(1.0)

  26.     def showEvent(self, event):
  27.         """Start update loop when window is shown"""
  28.         self.upload()
  29.         self.update_list.start()
  30.         event.accept()

  31.     def closeEvent(self, event):
  32.         """Stop update loop when window is closed"""
  33.         self.update_list.stop()
  34.         event.accept()

  35.     def upload(self):
  36.         """Forward update message to all ValueControl objects"""
  37.         if self.isVisible():
  38.             for child in self.children():
  39.                 if isinstance(child, ValueControl):
  40.                     child.update_value()
复制代码
代码是相同的。我想制作一个实现常见行为的超类,但是openEvent(),closeEvent()和self.children()都是新行为的一部分,并且这些方法是从QWidget继承的。
在C ++中,我将创建一个模板。您将如何在Python中做到这一点?

本帖寻求最佳方案

回复被采纳后将获得奖励 C币 5 ,目前已有 1 个回复 我要奖励
回复

使用道具 举报

5

主题

9

帖子

31

积分

新手上路

Rank: 1

积分
31
发表于 2021-5-18 00:28:38 | 显示全部楼层
您也许可以编写实现这三种方法的Mixin类。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 14:58 , Processed in 0.040315 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表