gpt4 book ai didi

python - 尝试覆盖 QTreeView.edit() 时出现最大递归错误

转载 作者:行者123 更新时间:2023-12-01 07:14:44 25 4
gpt4 key购买 nike

标题已经说明了大部分内容。我在这里必须是愚蠢的,但由于某种原因,每当我尝试覆盖 QTreeView 的编辑功能时,当我右键单击 Treeview 时,我都会收到最大递归深度错误。

这是特别是当我尝试调用 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继承自QAbstractItemView,如果你查看该类的方法,你可以看到有2个同名的方法:

bool QAbstractItemView::edit(const QModelIndex &index, QAbstractItemView::EditTrigger trigger, QEvent *event)

Starts editing the item at index, creating an editor if necessary, and returns true if the view's State is now EditingState; otherwise returns false.

The action that caused the editing process is described by trigger, and the associated event is specified by event.

Editing can be forced by specifying the trigger to be QAbstractItemView::AllEditTriggers.

<小时/>

void QAbstractItemView::edit(const QModelIndex &index)

Starts editing the item corresponding to the given index if it is editable.

Note that this function does not change the current index. Since the current index defines the next and previous items to edit, users may find that keyboard navigation does not work as expected. To provide consistent navigation behavior, call setCurrentIndex() before this function with the same model index.

据观察,第一种方法比第二种方法更通用,因此我们怀疑第二种方法将使用第一种方法,并且如果 the source code 确实会发生这种情况。已审核:

void QAbstractItemView::edit(const QModelIndex & index)
{
Q_D(QAbstractItemView);
if (Q_UNLIKELY(!d->isIndexIsValid(index)))
qWarning("edit: index was invalid");
if (Q_UNLIKELY(!<b>edit(index, AllEditTriggers, 0)</b>))
qWarning("edit: editing failed");
}

因此,在您的情况下,请清楚地解释错误:您正在重写第一个方法并调用第二个方法,但第二个方法根据源代码使用第一个方法,并返回到无限循环。

<小时/>

解决方案

解决方案是使用具有所有参数的相同方法的 super:

class EditTreeView(QtWidgets.QTreeView):
editingRequested = QtCore.pyqtSignal(QtCore.QModelIndex)

def edit(self, index, trigger, event):
self.editingRequested.emit(index)
return super(EditTreeView, self).edit(index, trigger, event)

但请记住,覆盖是关于第一个方法的,在 C++ 中允许有同名的方法,但在 python 中,如果有多个同名的方法,最后一个将删除前面的方法。

关于python - 尝试覆盖 QTreeView.edit() 时出现最大递归错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com