QTreeView: Expand item in 2nd column?
-
Hi guys,
I'm trying to build a treeview with a custom model that can be expanded on several columns.
According to the documentation and the illustrations about model indexes it should be possible. But I don't get it done. My first try with a dummy model should produce a tree like this, where item_2 is in the first row and second column and has one child (item_3):
item_1|item_2
.............|+ item_3My Code (Python):
@class TreeView(QtGui.QTreeView):def __init__(self, parent=None): super(TreeView, self).__init__(parent) model = TreeModel(self) self.setModel(model)
class TreeModel(QtCore.QAbstractItemModel):
def __init__(self, parent): super(TreeModel, self).__init__(parent) def rowCount(self, parent_index): if not parent_index.isValid(): return 1 parent_item = self.item_from_index(parent_index) if parent_item == "item_2": return 1 else: return 0 def columnCount(self, parent_index): return 2 def data(self, index, role): if role == QtCore.Qt.TextAlignmentRole: return int(QtCore.Qt.AlignTop|QtCore.Qt.AlignLeft) if role != QtCore.Qt.DisplayRole: return None return self.item_from_index(index) def headerData(self, section, orientation, role): if role != QtCore.Qt.DisplayRole: return None return "Items" def index(self, row, column, parent_index): parent_item = self.item_from_index(parent_index) if not parent_index.isValid(): if row == 0 and column == 0: return self.createIndex(row, column, "item_1") if row == 0 and column == 1: return self.createIndex(row, column, "item_2") if parent_item == "item_2": if row == 0 and column == 0: return self.createIndex(row, column, "item_3") return QtCore.QModelIndex() def parent(self, child_index): child_item = self.item_from_index(child_index) if child_item == "item_3": return self.createIndex(0, 1, "item_2") return QtCore.QModelIndex() def item_from_index(self, index): return (index.internalPointer() if index.isValid() else None)@
Do I have to set some flags to tell the view to query for expandable columns? Or is my model wrong or doesn't it work in general?
Any hints are appreciated. Also, any ideas how else to display such nested data without having to dive into stuff like GraphicViews?
Cheers,
Jan
-
QAbstractItemModel is quite flexible. It basicly describes a recursive table, where each cell in the table can be expanded into a completely new table recursively, and where each cell itself can hold several pieces of data (via the roles). However, Qt has no view that will display the whole structure, nor can I think of very many applications that would benefit from one. All views in Qt can display only substructures of the whole.
In effect, the datastructure is much too complicated for what it is supposed to do within Qt. That is one of problems of the QAbstractItemModel, I think. I think that is why for the QML models, they have gone with a much simpler model. Understandable and probably a good idea on the one hand (though not even supporting tables is a mistake, IMHO), but a pitty on the other. QML would have made it relatively easy to build a workable view for the whole structure, I think. It would have been doable to provide visual effects that make navigating a multi-leveled grid doable, like providing zoom-in effects with ease. That is hard to do properly with normal views...