[PyQt] QStandardItemModel best practice

Dom dom at reality-debug.co.uk
Sun Apr 18 17:43:18 BST 2010


I'm looking to create a table with checkable items based off a
dictionary using the QStandardItemModel class, what is the best practice
to do this? I've looked at using the class directly and it seemed
cumbersome to add the data to the table and I couldn't get data to
display at all when I subclassed.

class MyTableModel(QStandardItemModel):
    def __init__(self, datain, headerdata, parent=None, *args):
        """ datain: a list of lists headerdata: a list of strings """
        QStandardItemModel.__init__(self, 5, 6)
        self.arraydata = datain
        self.headerdata = headerdata
    def rowCount(self, parent):
        return len(self.arraydata)
    def columnCount(self, parent):
        return len(self.arraydata[0])
    def data(self, index, role):
        if not index.isValid():
            return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()
        return QVariant(self.arraydata[index.row()][index.column()])

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return QVariant(self.headerdata[col])
        return QVariant()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100418/a1c4df3e/attachment.html>

More information about the PyQt mailing list