Now when I have some MyModel I would like to write MyProxyModel which inherited by QAbstractProxyModel (I need to understand how they work that is why I don't want to use QSortFilterProxyModel for now).
QAbstractProxyModel inherits QAbstractItemModel so as I understand I must implelement at least the following pure virtual methods from these both abstract models:
int QAbstractItemModel::columnCount(const QModelIndex &parent = QModelIndex()) const
QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const
QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
QModelIndex QAbstractItemModel::parent(const QModelIndex &index) const
int QAbstractItemModel::rowCount(const QModelIndex &parent = QModelIndex()) const
QModelIndex QAbstractProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
QModelIndex QAbstractProxyModel::mapToSource(const QModelIndex &proxyIndex) const
Ok, I don't want to use any kind of a sorting I just want to wrap MyModel to MyProxyModel.
In proxy model I just declare the same methods that my source model has and invoke these methods:
h5proxymodel.h
#ifndef H5PROXYMODEL_H
#define H5PROXYMODEL_H
#include <QAbstractProxyModel>
#include <QSortFilterProxyModel>
#include "h5model.h"
class H5ProxyModel : public QAbstractProxyModel
{
Q_OBJECT
public:
explicit H5ProxyModel(QObject *parent = nullptr);
~H5ProxyModel();
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
void setSourceModel(QAbstractItemModel *sourceModel) override;
QAbstractItemModel* getSourceModel();
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
bool insertRows(int position, int rows,
const QModelIndex &parent = QModelIndex()) override;
bool removeRows(int position, int rows,
const QModelIndex &parent = QModelIndex()) override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
void expand(const QModelIndex &parent);
void collapse(const QModelIndex &parent);
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
QAbstractItemModel* sourceModel;
};
#endif // H5PROXYMODEL_H
h5proxymodel.cpp
#include "h5proxymodel.h"
H5ProxyModel::H5ProxyModel(QObject *parent) :
QAbstractProxyModel(parent)
{
}
H5ProxyModel::~H5ProxyModel()
{
delete sourceModel;
}
QModelIndex H5ProxyModel::mapFromSource(const QModelIndex &sourceIndex) const{
if (!sourceIndex.isValid())
return QModelIndex();
return sourceIndex;
}
QModelIndex H5ProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
if (!proxyIndex.isValid())
return QModelIndex();
return proxyIndex;
}
void H5ProxyModel::setSourceModel(QAbstractItemModel *sourceModel){
this->sourceModel = sourceModel;
}
QAbstractItemModel* H5ProxyModel::getSourceModel(){
return this->sourceModel;
}
QModelIndex H5ProxyModel::index(int row, int column, const QModelIndex &parent) const{
return this->sourceModel->index(row, column, parent);
}
QModelIndex H5ProxyModel::parent(const QModelIndex &index) const{
return this->sourceModel->parent(index);
}
int H5ProxyModel::rowCount(const QModelIndex &parent) const{
return this->sourceModel->rowCount(parent);
}
int H5ProxyModel::columnCount(const QModelIndex &parent) const{
return this->sourceModel->columnCount(parent);
}
QVariant H5ProxyModel::data(const QModelIndex &index, int role) const{
return sourceModel->data(index, role);
}
bool H5ProxyModel::setData(const QModelIndex &index, const QVariant &value, int role){
return sourceModel->setData(index, value, role);
}
QVariant H5ProxyModel::headerData(int section, Qt::Orientation orientation, int role) const{
return sourceModel->headerData(section, orientation, role);
}
bool H5ProxyModel::insertRows(int position, int rows, const QModelIndex &parent){
return sourceModel->insertRows(position, rows, parent);
}
bool H5ProxyModel::removeRows(int position, int rows, const QModelIndex &parent){
return sourceModel->removeRows(position, rows, parent);
}
bool H5ProxyModel::hasChildren(const QModelIndex &parent) const{
return sourceModel->hasChildren(parent);
}
void H5ProxyModel::expand(const QModelIndex &parent){
static_cast<H5Model*>(sourceModel)->expand(parent);
}
void H5ProxyModel::collapse(const QModelIndex &parent){
static_cast<H5Model*>(sourceModel)->collapse(parent);
}
Qt::ItemFlags H5ProxyModel::flags(const QModelIndex &index) const{
return sourceModel->flags(index);
}
As you can see all that my proxy does is it redirects commands to source model. But all I see is header in tree view instead of a items (the right picture I see if I set source model to a tree view):
[image: b30fd52f-a2f9-4466-83b8-a66cd89e5ede.png]
Why my logic doesn't work?