无法从 QAbstractItemModel 继承 [英] Unable to inherit from QAbstractItemModel
问题描述
我是 PyQt 中的模型/视图的新手,在创建从 QAbstractItemModel 类继承的对象时遇到了问题.这是从它继承的类:
I'm new to model/view in PyQt and I'm having trouble creating an object that inherits from the QAbstractItemModel class. Here's the class that inherits from it:
class MyCustomDataModel(QAbstractItemModel):
def __init(self, parent = None):
super(MyCustomDataModel, self).__init__(parent)
self.parent = parent
这是另一个尝试实例化上述类型对象的类:
Here's another class that tries to instantiate an object of the above type:
class MyCustomType(AnotherCustomTypeThatInheritsQListWidgetItem):
def __init__(self, parent = None):
super(MyCustomType, self).__init__(parent)
# Instantiate a model:
self.dataModel = MyCustomDataModel(self)
上面的实例化在运行时抛出以下错误:QAbstractItemModel(QObject parent = None): 参数 1 有意外的类型‘MyCustomType’"我看不出这里有什么问题,但我对 Python/PyQt 还是很陌生.有人可以指出我做错了什么吗?我试过调用实例化行,但没有传入任何内容,但出现相同的错误.
The above instantiation is throwing the following error at runtime: "QAbstractItemModel(QObject parent = None): argument 1 has unexpected type 'MyCustomType'" I cannot see what the problem is here, but I'm pretty new to Python / PyQt. Can someone please point out what I've done wrong? I've tried calling the instantiation line and not passing anything in but I get the same error.
推荐答案
这可能更适合作为评论而不是答案.
This might be more appropriate as a comment instead of an answer.
尝试更新将 MyCustomType 实例传递给 MyCustomDataModel 的行:
Try updating the line where you pass the MyCustomType instance to MyCustomDataModel:
self.dataModel = MyCustomDataModel(self)
改为使用:
self.dataModel = MyCustomDataModel()
这篇关于无法从 QAbstractItemModel 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!