Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QIdenitityProxyModel with more rows than source model

QIdenitityProxyModel with more rows than source model

Scheduled Pinned Locked Moved Unsolved General and Desktop
models
1 Posts 1 Posters 663 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrBolton
    wrote on 29 Jan 2016, 08:41 last edited by MrBolton
    #1

    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

    1 Reply Last reply
    0

    1/1

    29 Jan 2016, 08:41

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved