QIdenitityProxyModel with more rows than source model



  • I am trying to write a proxy model that adds a row to the source rather than filter it.
    I'm doing this in order to sum up the values of each column in the last row of the attached view.

    This is what I got so far (the class is derived from QIdentityProxyModel):

    #include "sumproxymodel.h"
    
    SumProxyModel::SumProxyModel(QObject *parent)
        : SUPER(parent)
    {}
    
    SumProxyModel::~SumProxyModel()
    {}
    
    QModelIndex SumProxyModel::index(int row, int column, const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        if(row > rowCount())
            return QModelIndex();
        else if(isLastRow(row))
            return createIndex(row, column);
        else
            return sourceModel()->index(row, column);
    }
    
    QModelIndex SumProxyModel::parent(const QModelIndex &child) const
    {
        Q_UNUSED(child)
        return QModelIndex();
    }
    
    QModelIndex SumProxyModel::mapToSource(const QModelIndex &proxyIndex) const
    {
        if(sourceModel() && proxyIndex.isValid()) {
            if(!isLastRow(proxyIndex.row()))
                return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
            else
                return proxyIndex;
        }
        else
            return QModelIndex();
    }
    
    QModelIndex SumProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
    {
        if(!sourceIndex.isValid())
            return QModelIndex();
    
        return index(sourceIndex.row(), sourceIndex.column());
    }
    
    int SumProxyModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return (sourceModel() ? sourceModel()->rowCount() + 1 : 0);
    }
    
    int SumProxyModel::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return (sourceModel() ? sourceModel()->columnCount() : 0);
    }
    
    QVariant SumProxyModel::data(const QModelIndex &index, int role) const
    {
        if(!sourceModel())
            return QVariant();
    
        if(role == Qt::DisplayRole
                && isLastRow(index.row())) {
    
            double sum = 0;
            int row = 0;
            while(!isLastRow(row)) {
                QVariant v = sourceModel()->index(row, index.column()).data();
                if(!v.canConvert(QMetaType::Double))
                    return QVariant();
                else {
                    sum += v.toDouble();
                    ++row;
                }
            }
    
            return sum;
        }
    
        return sourceModel()->data(index, role);
    }
    
    QVariant SumProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if(orientation == Qt::Vertical
                && role == Qt::DisplayRole) {
            if(isLastRow(section))
                return tr("Sum");
        }
    
        return SUPER::headerData(section, orientation, role);
    }
    
    bool SumProxyModel::isLastRow(int row) const
    {
        if(!sourceModel())
            return false;
        return row == rowCount() - 1;
    }
    

    This works as long as the source model is a QAbstractTableModel. But when I add a QSortFilterProxyModel inbetween the two models, it crashes. I guess this is due to the last row not being static anymore when using a sorting model. But how can I get around the problem? Any help is appreciated!

    Tobi


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.