[PyQt] QAbstractTableModel.data() implemented but get still

Michael Hammer michael at derhammer.net
Fri Jan 23 12:28:40 GMT 2009


Hi folks!

I've really tried a lot of different things - and I found one often proposed
solution to keep the instance of the model alive. As I can't find any error,
perhaps some of you can help.

The MainWindow where the model and view are referenced:

class MainWindow(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)

        ....

        self.model = table.TableModel([])
        self.table_view = table.TableView()
        self.table_view.setModel(self.model)
        self.ui.gridlayout.addWidget(self.table_view, 0, 0, 1, 1)

And here is the model - I've indeed implemented the data() method:

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self,header,parent=None):
        QtCore.QAbstractTableModel.__init__(self,parent)
        self.header = ['a','b','c']
        self.data = [['y','test1'],['n','test2'],['y','test3']]

    def columnCount(self,parent):
        return len(self.header)

    def rowCount(self,parent):
        return len(self.data)

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

    def data(self, index, role):
        if not index.isValid():
            return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()
        return QVariant(self.data[index.row()][index.column()])

and there is some view to it:

class TableView(QtGui.QTableView):
    def __init__(self,parent=None):
        QtGui.QTableView.__init__(self,parent)

but I still get the error:

NotImplementedError: QAbstractTableModel.data() is abstract and must be 
overridden

Thx for any hint, greets

Michael




More information about the PyQt mailing list