尝试重写QTreeView.edit()时出现最大递归错误

2024-09-30 16:41:30 发布

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

标题说明了大部分。我必须在这里是一些愚蠢的东西,但出于某种原因,每当我试图覆盖编辑功能的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_()

Tags: selfnone编辑lomodelinitdefparent
1条回答
网友
1楼 · 发布于 2024-09-30 16:41:30

说明:

QTreeView继承自qabstractemview,如果查看该类的方法,可以看到有两个同名的方法:

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(!edit(index, AllEditTriggers, 0)))
        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中,如果有几个同名方法,最后一个将擦除以前的方法。p>

相关问题 更多 >