从Python中的子类访问父函数/变量

2024-03-29 02:11:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试使用Python和QT为Maya(3D应用程序)创建一些工具

到目前为止,我还过得很好,但现在我遇到了一些问题,开始考虑继承问题。我创建了QListWidgetItem的子类来添加一些自定义逻辑,并希望在稍后迭代左侧QListWidget时访问此逻辑

My UI

# Widget to get displayed inside myQListWidget
class BaseModule(QtWidgets.QListWidgetItem):
   def __init__(self,name):
      super(BaseModule, self).__init__(parent=parent)
         self.setText(name)
         self.name = name

   def BuildSkeleton(self):
      # Do some magic in here

window = belt.TWToolbelt()
rootModule = BaseModule(name='Root')
window.ui.AllModules.addItem(rootModule)

model = window.ui.ActiveModules.model()
model.rowsInserted.connect(lambda *_:drop(window))

# function, which get`s executed after a drag&drop action    
def drop(window):
   items = []
   for index in xrange(window.ui.ActiveModules.count()):
      items.append(window.ui.ActiveModules.item(index))

   for item in items:
      # iterating over all Active Modes on the left to execute my custom logic
      # as item is a QtWidget.QListWidgetItem and the fucntion is only existing
      # in it`s child class BaseModule I would like to know, how I can access or cast from a 
      # ParentClass to it`s child class, so I can execute my function
      item.BuildSkeleton()

我发现该代码有以下错误:

 AttributeError: 'PySide2.QtWidgets.QListWidgetItem' object has no attribute 'BuildSkeleton'

希望你能帮助我

先谢谢你


Tags: tonameinselfuimodeldefwindow