Using proxy models
        Posted  
        
            by 
                smallB
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by smallB
        
        
        
        Published on 2011-11-26T17:39:54Z
        Indexed on 
            2011/11/26
            17:51 UTC
        
        
        Read the original article
        Hit count: 260
        
I've created Proxy model by subclassing QAbstractProxyModel and connected it as a model to my view. I also set up source model for this proxy model. Unfortunately something is wrong because I'm not getting anything displayed on my listView (it works perfectly when I have my model supplied as a model to view but when I supply this proxy model it just doesn't work). Here are some snippets from my code:
#ifndef FILES_PROXY_MODEL_H
#define FILES_PROXY_MODEL_H
#include <QAbstractProxyModel>
#include "File_List_Model.h"
class File_Proxy_Model: public QAbstractProxyModel
{
public:
    explicit File_Proxy_Model(File_List_Model* source_model)
{
    setSourceModel(source_model);
}
    virtual QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
    {
        return index(sourceIndex.row(),sourceIndex.column());
    }
    virtual QModelIndex mapToSource(const QModelIndex & proxyIndex) const
    {
            return index(proxyIndex.row(),proxyIndex.column());
    }
    virtual int columnCount(const QModelIndex & parent = QModelIndex()) const
    {
        return sourceModel()->columnCount();
    }
    virtual int rowCount(const QModelIndex & parent = QModelIndex()) const
    {
        return sourceModel()->rowCount();
    }
    virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
    {
        return createIndex(row,column);
    }
    virtual QModelIndex parent(const QModelIndex & index) const
    {
        return QModelIndex();
    }
};
#endif // FILES_PROXY_MODEL_H
//and this is a dialog class:  
Line_Counter::Line_Counter(QWidget *parent) :
    QDialog(parent), model_(new File_List_Model(this)),
                     proxy_model_(new File_Proxy_Model(model_)),
                     sel_model_(new QItemSelectionModel(proxy_model_,this))
{
    setupUi(this);
    setup_mvc_();
}
void Line_Counter::setup_mvc_()
{
    listView->setModel(proxy_model_);
    listView->setSelectionModel(sel_model_);
}
© Stack Overflow or respective owner