Short version
I am trying to display data from a QAbstracctTableModel
in a QTreeView
. When I do, I end up with the entire table displayed as the child of every node (and grandchild, and so-on). How can I display a treeview of an abstract table model?
Details
I am trying to display some data in a QAbstractTableModel
in a QTreeView
. In the Model-View Tutorial, after presenting an example QAbstractTableModel
, it makes it seem it's as simple as replacing QTableView
with QTreeView
:
You can convert the example above into an application with a tree view. Simply replace QTableView with QTreeView, which results in a read/write tree. No changes have to be made to the model.
When I make this replacement, I do end up with a tree showing, but if I click on any of the icons to expand it (which should do nothing, as there is no hierarchy built in), Python crashes with Python.exe has stopped working
. This has been brought up before ], but without a workable solution.
To try to fix this behavior, I reimplemented the index function in my QAbstractTableModel
subclass (see full working example below). This leads to a very different type of error. Namely, every node in the tree now contains the entire table as data. No matter how many times I click, the entire table shows up. Like this:
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()