错误?在PyQt5中,为QStandardItemModel实现rowCount()具有奇怪的效果

问题描述 投票:0回答:1

我只是想针对QStandardItemModel实施并试图使rowCount()返回|行| + 1显示额外的一行。我将相应地实现data()flags()以显示多余行的有效值。

但是现在我很困惑。画了多余的​​线(因此该方法正确实现),但是data()flags()总是用内部数据模型内的索引调用。另外,我的应用程序在大约50%的情况下崩溃。

因此,看起来rowCount()并非在所有使用[[应该的情况下都被使用,这导致对内部数据的无效访问。]]这是一个错误吗?

我正在Linux(Ubuntu 18.04)上使用PyQt5-5.14.0

from PyQt5 import QtWidgets, QtGui, QtCore class MyModel(QtGui.QStandardItemModel): def __init__(self): super().__init__() self.appendRow([QtGui.QStandardItem("Hi"), QtGui.QStandardItem("there")]) self.appendRow([QtGui.QStandardItem("Hello"), QtGui.QStandardItem("world")]) def rowCount(self, parent): return super().rowCount() + 1 def data(self, index: QtCore.QModelIndex, role: QtCore.Qt.ItemDataRole): assert index.row() < 2 # index.row() is always in [-1, 0, 1] return super().data(index, role) def flags(self, index: QtCore.QModelIndex) -> QtCore.Qt.ItemFlags: assert index.row() < 2 # index.row() is always in [-1, 0, 1] return super().flags(index) class Testing(QtWidgets.QMainWindow): def __init__(self): super().__init__() model = MyModel() view = QtWidgets.QTableView(self) view.setModel(model) self.setCentralWidget(view) self.show() if __name__ == '__main__': app = QtWidgets.QApplication([]) test = Testing() raise SystemExit(app.exec_())

我只是想针对QStandardItemModel实现,并试图使rowCount()返回|行|。 + 1显示额外的一行。我将相应地实现data()和flags()以显示有效值...
python-3.x pyqt pyqt5
1个回答
0
投票
我忘了实现index()
© www.soinside.com 2019 - 2024. All rights reserved.