Hello,<br><br>I'm attempting to implement a QAbstractTableModel for a QTableView. The data im attemptingto model is a single column hiarchy.<br><br>Say I have the following data:<br><br>unasigned<br>foo<br> bar<br>
baby<br><br>When I model this data with the below code, I get the ouput:<br><br>unasigned<br>foo<br> unassigned<br><br>baby never shows up, and the unassigned under foo is unexpandable.<br><br>Below is the code I'm using, I think the problem is with how I'm building the FolderItem structure, however when i add debug code, it seems to be building it correctly. Any help with what I'm doing wrong would be appreciated. Note my code is straight out of the simpletreeview example in the pyqt archive.<br>
<br><-- begin code --><br><br>class FolderItem:<br> def __init__(self, data, parent = None):<br> self.parentItem = parent<br> self.itemData = data<br> self.childItems = []<br> <br> def appendChild(self, item):<br>
self.childItems.append(item)<br> <br> def child(self, row):<br> return self.childItems[row]<br> <br> def childCount(self):<br> return len(self.childItems)<br><br> def row(self):<br> if self.parentItem:<br>
return self.parentItem.childItems.index(self)<br> return 0<br><br> def columnCount(self):<br> return len(self.itemData)<br><br> def data(self, column):<br> return self.itemData[column]<br>
<br> def parent(self):<br> return self.parentItem<br><br>class FolderModel(QtCore.QAbstractTableModel):<br> def __init__(self, data, parent = None, *args):<br> QtCore.QAbstractTableModel.__init__(self, parent, *args)<br>
self.rootItem = self.generateItemList(data)<br><br> def generateItemList(self, data, parent = None):<br> top = None<br> if parent == None:<br> header = data.pop(0);<br> top = FolderItem(header)<br>
top = self.generateItemList(data, top)<br> elif parent:<br> tmp = None<br> top = parent<br> for path in data:<br> myparent = top<br> pieces = path.split('/')<br>
pieces.pop(0)<br><br> for p in pieces:<br> print myparent.data(0) + " -> " + p<br> tmp = FolderItem([p], myparent)<br> myparent.appendChild(tmp)<br>
myparent = tmp<br> return top<br><br> def index(self, row, column, parent):<br> if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):<br>
return QtCore.QModelIndex()<br><br> if not parent.isValid():<br> parentItem = self.rootItem<br> else:<br> parentItem = parent.internalPointer()<br><br> childItem = parentItem.child(row)<br>
if childItem:<br> return self.createIndex(row, column, childItem)<br> else:<br> return QtCore.QModelIndex()<br><br> def parent(self, index):<br> if not index.isValid():<br> return QtCore.QModelIndex()<br>
<br> childItem = index.internalPointer()<br> parentItem = childItem.parent()<br><br> if parentItem == self.rootItem:<br> return QtCore.QModelIndex()<br> return self.createIndex(parentItem.row(), 0, parentItem)<br>
<br> def rowCount(self, parent):<br> if parent.column() > 0:<br> return 0<br> if not parent.isValid():<br> parentItem = self.rootItem<br> else:<br> parentItem = parent.internalPointer()<br>
<br> return parentItem.childCount()<br><br> def columnCount(self, parent):<br> if parent.isValid():<br> return parent.internalPointer().columnCount()<br> return self.rootItem.columnCount()<br>
<br> def data(self, index, role):<br> if not index.isValid():<br> return QtCore.QVariant()<br> if role != QtCore.Qt.DisplayRole:<br> return QtCore.QVariant()<br> <br> item = index.internalPointer()<br>
return QtCore.QVariant(item.data(index.column()))<br> <br> def flags(self, index):<br> if not index.isValid():<br> return QtCore.Qt.ItemIsEnabled<br> return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable<br>
<br> def headerData(self, section, orientation, role):<br> if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:<br> return QtCore.QVariant(self.rootItem.data(section))<br> return QtCore.QVariant()<br>
<br><-- end code --><br><br><-- begin example usage --><br><br>data = []<br>data.append(["Column Header"])<br>data.append("/Unassigned")<br>data.append("/foo/bar/baby")<br><br>self.ui.foldermodel = FolderModel(data)<br>
<br><-- end example usage --><br><br><br>Thanks in advance,<br>Ben<br>