[PyQt] Trouble implementing QAbstractTableModel

comsatcat comsatcat at gmail.com
Thu Oct 23 07:49:55 BST 2008


Hello,

I'm attempting to implement a QAbstractTableModel for a QTableView.  The
data im attemptingto model is a single column hiarchy.

Say I  have the following data:

unasigned
foo
     bar
         baby

When I model this data with the below code, I get the ouput:

unasigned
foo
    unassigned

baby never shows up, and the unassigned under foo is unexpandable.

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.

<-- begin code -->

class FolderItem:
    def __init__(self, data, parent = None):
        self.parentItem = parent
        self.itemData = data
        self.childItems = []

    def appendChild(self, item):
        self.childItems.append(item)

    def child(self, row):
        return self.childItems[row]

    def childCount(self):
        return len(self.childItems)

    def row(self):
        if self.parentItem:
                return self.parentItem.childItems.index(self)
        return 0

    def columnCount(self):
        return len(self.itemData)

    def data(self, column):
        return self.itemData[column]

    def parent(self):
        return self.parentItem

class FolderModel(QtCore.QAbstractTableModel):
    def __init__(self, data, parent = None, *args):
        QtCore.QAbstractTableModel.__init__(self, parent, *args)
        self.rootItem = self.generateItemList(data)

    def generateItemList(self, data, parent = None):
        top = None
        if parent == None:
            header = data.pop(0);
            top = FolderItem(header)
            top = self.generateItemList(data, top)
        elif parent:
            tmp = None
            top = parent
            for path in data:
                myparent = top
                pieces = path.split('/')
                pieces.pop(0)

                for p in pieces:
                    print myparent.data(0) + " -> " + p
                    tmp = FolderItem([p], myparent)
                    myparent.appendChild(tmp)
                    myparent = tmp
        return top

    def index(self, row, column, parent):
        if row < 0 or column < 0 or row >= self.rowCount(parent) or column
>= self.columnCount(parent):
                return QtCore.QModelIndex()

        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()

        childItem = parentItem.child(row)
        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()

    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()

        childItem = index.internalPointer()
        parentItem = childItem.parent()

        if parentItem == self.rootItem:
            return QtCore.QModelIndex()
        return self.createIndex(parentItem.row(), 0, parentItem)

    def rowCount(self, parent):
        if parent.column() > 0:
            return 0
        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()

        return parentItem.childCount()

    def columnCount(self, parent):
        if parent.isValid():
                return parent.internalPointer().columnCount()
        return self.rootItem.columnCount()

    def data(self, index, role):
        if not index.isValid():
            return QtCore.QVariant()
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        item = index.internalPointer()
        return QtCore.QVariant(item.data(index.column()))

    def flags(self, index):
        if not index.isValid():
            return QtCore.Qt.ItemIsEnabled
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role ==
QtCore.Qt.DisplayRole:
            return QtCore.QVariant(self.rootItem.data(section))
        return QtCore.QVariant()

<-- end code -->

<-- begin example usage -->

data = []
data.append(["Column Header"])
data.append("/Unassigned")
data.append("/foo/bar/baby")

self.ui.foldermodel = FolderModel(data)

<-- end example usage -->


Thanks in advance,
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20081023/39e4d10c/attachment.html


More information about the PyQt mailing list