+3  A: 

Because QTableView uses the model index to retrieve the data, probably something like this.

QModelIndex index = model->index(row, column, parentIndex); 
index.data(Qt::DisplayRole);

And you are returning the model index of the source model instead of an index to your proxy model:

QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
    return this->sourceModel()->index(row,column,parent);
}

Try to convert the model index into an index to your proxy model

QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
    return this->createIndex(row,column,row);
}

Don't forget to rewrite the map to source and map from source functions.


Solution

class MyTableProxyModel : public QAbstractProxyModel
{
    Q_OBJECT
public:
    MyTableProxyModel (QObject* parent = 0) 
        : QAbstractProxyModel(parent) {
    }

    QModelIndex index(int row, int column, const QModelIndex& parent=QModelIndex()) const {
        return createIndex(row,column,row);
    }

    QModelIndex parent(const QModelIndex &index) const {
        //Works only for non-tree models
        return QModelIndex();
    }

    QModelIndex mapFromSource(const QModelIndex &source) const {
        return index(source.row(), source.column(), source.parent());
    }

    QModelIndex mapToSource(const QModelIndex &proxy) const {
        return (sourceModel()&&proxy.isValid())
            ? sourceModel()->index(proxy.row(), proxy.column(), proxy.parent()) 
            : QModelIndex();
    }

    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
        qDebug() << "myproxymodel data";
        return mapToSource(index).data(role);
    }

    int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
        return sourceModel() ? sourceModel()->rowCount(parent) : 0;
    }

    int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
        return sourceModel() ? sourceModel()->columnCount(parent) : 0;
    }
};
TimW
Ok, I see. I have then 2 questions:- How can i achieve my goal without creating proxy indexes? I'd like to avoid creating indexes in the proxy model, because there won't be any filtering/sorting, etc, and that would be a complete duplication of the original models's indexes.- I don't see how the `QSortFilterProxyModel` can help me out. I have to code the `data(..)` function, and because of the above, I need also code the `index(...)` too. What `QSortFilterProxyModel` does that `QAbstractProxyModel` doesn't in my point of view?
You need to create new indexes that are pointing to the proxy model. QSortFilterProxyMode creates the proxy indexes for you.
TimW
Thank you very much, the mapToSource and mapFromSource were the devil's eyes for me! That works like a charm!