Sure, I attached the proxyFilter code. What's also interessting that if I click into the treeview I get a bunch of cout's from the setData instead just a single one.
snip main.cpp
TestModel model;
table->setModel(&model);
ProxyFilter filterModel;
filterModel.setSourceModel(&model);
tree->setModel(&filterModel);
splitter->setWindowTitle("Test views");
splitter->show();
return app.exec();
}
snip main.cpp
TestModel model;
QSplitter *splitter = new QSplitter;
QTreeView *tree = new QTreeView(splitter);
QTableView *table = new QTableView(splitter);
table->setModel(&model);
table->setRootIndex(QModelIndex());
ProxyFilter filterModel;
filterModel.setSourceModel(&model);
tree->setModel(&filterModel);
splitter->setWindowTitle("Test views");
splitter->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
proxyFilter.cpp:
#include "proxyFilter.h"
#include <iostream>
ProxyFilter::ProxyFilter() {
}
ProxyFilter::~ProxyFilter() {
}
std::cout << "mapFromSource" << std::endl;
if(sourceIndex.isValid())
return createIndex(sourceIndex.row(), sourceIndex.column());
else
}
std::cout << "mapToSource" << std::endl;
return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
}
int ProxyFilter
::columnCount(const QModelIndex & parent
) const {
// return sourceModel()->columnCount(parent);
// filter out all cols - keep only col = 0
return 1;
}
int ProxyFilter
::rowCount(const QModelIndex & parent
) const { return sourceModel()->rowCount(parent);
}
QModelIndex ProxyFilter
::index(int row,
int column,
const QModelIndex
& parent
) const {
return sourceModel()->index(row, column, parent);
}
QModelIndex ProxyFilter
::parent(const QModelIndex
&) const {
}
std::cout << "data: row =" << index.row() << " col=" << index.column() << std::endl;
return sourceModel()->data(index, role);
}
std::cout << "setData: row =" << index.row() << " col=" << index.column() << std::endl;
return sourceModel()->setData(index, value, role);
}
Qt
::ItemFlags ProxyFilter
::flags(const QModelIndex &index
) const {
return sourceModel()->flags(index);
}
connect(sourceModel, SIGNAL(modelReset()), this, SIGNAL(modelReset()));
}
#include "proxyFilter.h"
#include <iostream>
ProxyFilter::ProxyFilter() {
}
ProxyFilter::~ProxyFilter() {
}
QModelIndex ProxyFilter::mapFromSource (const QModelIndex & sourceIndex) const {
std::cout << "mapFromSource" << std::endl;
if(sourceIndex.isValid())
return createIndex(sourceIndex.row(), sourceIndex.column());
else
return QModelIndex();
}
QModelIndex ProxyFilter::mapToSource (const QModelIndex & proxyIndex) const {
std::cout << "mapToSource" << std::endl;
return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
}
int ProxyFilter::columnCount(const QModelIndex & parent) const {
// return sourceModel()->columnCount(parent);
// filter out all cols - keep only col = 0
return 1;
}
int ProxyFilter::rowCount(const QModelIndex & parent) const {
return sourceModel()->rowCount(parent);
}
QModelIndex ProxyFilter::index(int row, int column, const QModelIndex& parent) const {
return sourceModel()->index(row, column, parent);
}
QModelIndex ProxyFilter::parent(const QModelIndex&) const {
return QModelIndex();
}
QVariant ProxyFilter::data(const QModelIndex & index, int role) const {
std::cout << "data: row =" << index.row() << " col=" << index.column() << std::endl;
return sourceModel()->data(index, role);
}
bool ProxyFilter::setData(const QModelIndex &index, const QVariant &value, int role) {
std::cout << "setData: row =" << index.row() << " col=" << index.column() << std::endl;
return sourceModel()->setData(index, value, role);
}
Qt::ItemFlags ProxyFilter::flags(const QModelIndex &index) const {
return sourceModel()->flags(index);
}
void ProxyFilter::setSourceModel(QAbstractItemModel *sourceModel) {
connect(sourceModel, SIGNAL(modelReset()), this, SIGNAL(modelReset()));
QAbstractProxyModel::setSourceModel(sourceModel);
}
To copy to clipboard, switch view to plain text mode
proxyFilter.h:
#ifndef PROXYFILTER_H
#define PROXYFILTER_H
#include <QAbstractProxyModel>
#include <QAbstractItemModel>
Q_OBJECT
public:
ProxyFilter();
~ProxyFilter();
};
#endif
#ifndef PROXYFILTER_H
#define PROXYFILTER_H
#include <QAbstractProxyModel>
#include <QAbstractItemModel>
class ProxyFilter : public QAbstractProxyModel {
Q_OBJECT
public:
ProxyFilter();
~ProxyFilter();
int columnCount(const QModelIndex & parent = QModelIndex()) const;
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QModelIndex mapFromSource (const QModelIndex & sourceIndex) const;
QModelIndex mapToSource (const QModelIndex & proxyIndex) const;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex&) const ;
QVariant data(const QModelIndex & index, int role) const;
bool setData (const QModelIndex &index, const QVariant &value, int role);
Qt::ItemFlags flags(const QModelIndex &index) const;
void setSourceModel(QAbstractItemModel *sourceModel);
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks