标题说明了大部分。我必须在这里是一些愚蠢的东西,但出于某种原因,每当我试图覆盖编辑功能的QTreeView当我右键点击树视图,我得到一个最大的递归深度错误
这是特别当我试图调用super的编辑函数时。这是一个问题的例子。我在搞什么鬼
from PyQt5 import QtWidgets, QtGui, QtCore
class EditTreeView(QtWidgets.QTreeView):
editingRequested = QtCore.pyqtSignal(QtCore.QModelIndex)
def __init__(self, parent=None):
super(EditTreeView, self).__init__(parent)
def edit(self, index, QAbstractItemView_EditTrigger=None, QEvent=None):
super(EditTreeView, self).edit(index)
class testTreeview(QtWidgets.QWidget):
def __init__(self, parent=None):
super(testTreeview, self).__init__(parent)
self.mainTree = EditTreeView()
self.lo = QtWidgets.QVBoxLayout()
self.lo.addWidget(self.mainTree)
self.setLayout(self.lo)
self.model = QtGui.QStandardItemModel()
self.mainTree.setModel(self.model)
def populate(self):
row = [QtGui.QStandardItem('teststuff'), ]
root = self.model.invisibleRootItem()
root.appendRow(row)
if __name__ == "__main__":
from PyQt5 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication([])
volume = testTreeview()
volume.show()
app.exec_()
说明:
QTreeView继承自qabstractemview,如果查看该类的方法,可以看到有两个同名的方法:
据观察,第一种方法比第二种方法更为普遍,因此我们怀疑第二种方法会使用第一种方法,如果对the source code进行审查,确实会发生这种情况:
所以在您的例子中,请清楚地解释错误:您正在重写第一个方法并调用第二个方法,但是第二个方法根据源代码使用第一个方法,并返回到无限循环
解决方案
解决方案是对所有参数使用同一方法的super:
请记住,重写是第一种方法,在C++中,允许使用同名的方法,但在Python中,如果有几个同名方法,最后一个将擦除以前的方法。p>相关问题 更多 >
编程相关推荐