精简版
我试图从显示的数据QAbstracctTableModel
中QTreeView
。完成后,最终将整个表显示为每个节点的子代(以及孙子代,等等)。如何显示抽象表模型的树状视图?
细节
我想在显示一些数据QAbstractTableModel
中QTreeView
。在模型-视图教程,呈现一个例子后QAbstractTableModel
,这使它看起来很简单的替换QTableView
用QTreeView
:
您可以将上面的示例转换为具有树视图的应用程序。只需将QTableView替换为QTreeView,即可生成一个读/写树。无需对模型进行任何更改。
进行此替换时,最终会显示一棵树,但是如果单击任何图标以将其展开(由于没有内置层次结构,该图标将不起作用),Python崩溃Python.exe has stopped working
。这是在]之前提出的,但是没有可行的解决方案。
为了尝试解决此问题,我在QAbstractTableModel
子类中重新实现了索引功能(请参见下面的完整工作示例)。这导致了非常不同的错误类型。即,树中的每个节点现在都包含整个表作为数据。无论我单击多少次,都将显示整个表格。像这样:
I seem to be in some kind of recursive nightmare, and do not know how to escape. The related question below suggests I might have to go to QAbstractItemModel
, but the tutorial quote above suggests otherwise (which states, No changes have to be made to the model).
Related question
QTreeView always displaying the same data
Full working example
from PySide import QtGui, QtCore
class Food(object):
def __init__(self, name, shortDescription, note, parent = None):
self.data = (name, shortDescription, note);
self.parentIndex = parent
class FavoritesTableModel(QtCore.QAbstractTableModel):
def __init__(self):
QtCore.QAbstractTableModel.__init__(self)
self.foods = []
self.loadData()
def data(self, index, role = QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
return self.foods[index.row()].data[index.column()]
return None
def rowCount(self, index=QtCore.QModelIndex()):
return len(self.foods)
def columnCount(self, index=QtCore.QModelIndex()):
return 3
def index(self, row, column, parent = QtCore.QModelIndex()):
return self.createIndex(row, column, parent)
def loadData(self):
allFoods=("Apples", "Pears", "Grapes", "Cookies", "Stinkberries")
allDescs = ("Red", "Green", "Purple", "Yummy", "Huh?")
allNotes = ("Bought recently", "Kind of delicious", "Weird wine grapes",
"So good...eat with milk", "Don't put in your nose")
for name, shortDescription, note in zip(allFoods, allDescs, allNotes):
food = Food(name, shortDescription, note)
self.foods.append(food)
def main():
import sys
app = QtGui.QApplication(sys.argv)
model = FavoritesTableModel()
#Table view
view1 = QtGui.QTableView()
view1.setModel(model)
view1.show()
#Tree view
view2 = QtGui.QTreeView()
view2.setModel(model)
view2.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
From the official documentation:
When implementing a table based model,
rowCount()
should return 0 when the parent is valid.
The same goes for columnCount()
. And for completeness, data()
should return None
if the parent is valid.
What happened is this:
rowCount()
, and passes the index of the "Stinkberries" cell as the parent.FavoritesTableModel::rowCount()
returns 5, so the QTreeView thinks, "Ah, there are 5 rows under 'Stinkberries'."data()
, passing Row 0, Column 0, and the index of the "Stinkberries" cell as the parent.FavoritesTableModel::data()
returns "Apples", so the QTreeView thinks, "Ah, the first item under 'Stinkberries' is 'Apples'."为了获得正确的行为,您的代码必须为步骤3和步骤4返回0。
最后,要确保根本不显示“ +”号,请hasChildren()
对每个单元格都返回false。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句