I want to create a Proxy Model - as a first step I tried to create a Proxy that should behave exaclty like the sourceModel:
{
Q_OBJECT
public:
{
return sourceIndex;
}
{
return proxyIndex;
}
int rowCount(const QModelIndex& parent) const
{
return sourceModel()->rowCount(parent);
}
int columnCount(const QModelIndex& parent) const
{
return sourceModel()->columnCount(parent);
}
QModelIndex index
(int row,
int col,
const QModelIndex
& parent
) const {
return sourceModel()->index(row, col, parent);
}
{
return parent(index);
}
};
class Proxy : public QAbstractProxyModel
{
Q_OBJECT
public:
Proxy(QObject* parent = 0) : QAbstractProxyModel(parent) { }
QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const
{
return sourceIndex;
}
QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const
{
return proxyIndex;
}
int rowCount(const QModelIndex& parent) const
{
return sourceModel()->rowCount(parent);
}
int columnCount(const QModelIndex& parent) const
{
return sourceModel()->columnCount(parent);
}
QModelIndex index(int row, int col, const QModelIndex& parent) const
{
return sourceModel()->index(row, col, parent);
}
QModelIndex parent(const QModelIndex& index) const
{
return parent(index);
}
};
To copy to clipboard, switch view to plain text mode
So far one thing is unclear to me: why do I have to implement rowCount, columnCount, index and parent? Why isn't this done in QAbstractProxyModel?
when using my proxy with the simpletreemodel-example this way:
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(simpletreemodel);
QFile file(":/default.txt");
TreeModel model(file.readAll());
file.close();
Proxy proxy;
proxy.setSourceModel(&model);
view.setModel(&proxy);
view.
setWindowTitle(QObject::tr("Simple Tree Model"));
view.show();
return app.exec();
}
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(simpletreemodel);
QApplication app(argc, argv);
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
TreeModel model(file.readAll());
file.close();
Proxy proxy;
proxy.setSourceModel(&model);
QTreeView view;
view.setModel(&proxy);
view.setWindowTitle(QObject::tr("Simple Tree Model"));
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I get a messed-up tree - see attached screenshot.
Using a QTableView however works!
please help
niko
Bookmarks