PyQt - trouble with reimplementing data method of QSqlTableModel
<p>I'm a newbie with python and mainly with pyqt. The problem is simple: I have a <code>QTableView</code> and I want to "simply" change the color of some rows. Reading all around I found that the simplest solution should be to override the data method in the model in such a way:</p>

<pre><code>class MyModel(QtSql.QSqlTableModel):

    def data(self,idx,role):
        testindex=self.index(idx.row(),idx.column(),idx.parent())
        if(role==QtCore.Qt.BackgroundRole):
            return QtGui.QColor(255,0,0)
        elif role == QtCore.Qt.DisplayRole:
            return QtSql.QSqlTableModel().data(testindex)
</code></pre>

<p>When I use this model reimplementation, the rows are changing color but the cell values disappear and the return statement <code>QtSql.QSqlTableModel().data(testindex)</code> is always <code>None</code>.
I'm getting crazy to find out a solution. Could you help me?</p>

<p>Your implementation is broken in a couple of ways: (1) it always returns <code>None</code> for any unspecified roles, (2) it creates a new instance of <code>QSqlTableModel</code> every time the display role is requested, instead of calling the base-class method.</p>

<p>The implementation should probably be something like this:</p>

<pre><code>class MyModel(QtSql.QSqlTableModel):

    def data(self, index, role):
        if role == QtCore.Qt.BackgroundRole:
            return QtGui.QColor(255, 0, 0)
        return super(MyModel, self).data(index, role)
</code></pre>

python pyqt override qsqltablemodel