#include <QtGui>
#include <QtSql>
#include <QApplication>
// Add one extra column at the beginning
{
Q_OBJECT
public:
explicit ProxyModel
(QObject * parent
= 0) {
;
}
{
if(this->sourceModel() != sourceModel)
{
if(this->sourceModel() != 0)
{
disconnect
(this
->sourceModel
(),
SIGNAL(rowsAboutToBeInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(rowsAboutToBeInserted
(const QModelIndex &,
int,
int)));
disconnect
(this
->sourceModel
(),
SIGNAL(rowsInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(rowsInserted
(const QModelIndex &,
int,
int)));
disconnect
(this
->sourceModel
(),
SIGNAL(columnsAboutToBeInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(columnsAboutToBeInserted
(const QModelIndex &,
int,
int)));
disconnect
(this
->sourceModel
(),
SIGNAL(columnsInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(columnsInserted
(const QModelIndex &,
int,
int)));
disconnect(this->sourceModel(), SIGNAL(modelAboutToBeReset()),
this, SIGNAL(modelAboutToBeReset()));
disconnect(this->sourceModel(), SIGNAL(modelReset()),
this, SIGNAL(modelReset()));
disconnect(this->sourceModel(), SIGNAL(layoutAboutToBeChanged()),
this, SIGNAL(layoutAboutToBeChanged()));
disconnect(this->sourceModel(), SIGNAL(layoutChanged()),
this, SIGNAL(layoutChanged()));
}
connect(this
->sourceModel
(),
SIGNAL(rowsAboutToBeInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(rowsAboutToBeInserted
(const QModelIndex &,
int,
int)));
connect(this
->sourceModel
(),
SIGNAL(rowsInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(rowsInserted
(const QModelIndex &,
int,
int)));
connect(this
->sourceModel
(),
SIGNAL(columnsAboutToBeInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(columnsAboutToBeInserted
(const QModelIndex &,
int,
int)));
connect(this
->sourceModel
(),
SIGNAL(columnsInserted
(const QModelIndex &,
int,
int)),
this,
SIGNAL(columnsInserted
(const QModelIndex &,
int,
int)));
connect(this->sourceModel(), SIGNAL(modelAboutToBeReset()),
this, SIGNAL(modelAboutToBeReset()));
connect(this->sourceModel(), SIGNAL(modelReset()),
this, SIGNAL(modelReset()));
connect(this->sourceModel(), SIGNAL(modelReset()),
this, SIGNAL(modelReset()));
connect(this->sourceModel(), SIGNAL(layoutAboutToBeChanged()),
this, SIGNAL(layoutAboutToBeChanged()));
connect(this->sourceModel(), SIGNAL(layoutChanged()),
this, SIGNAL(layoutChanged()));
}
}
{
if(!parent.isValid())
{
if(column == 0)
return createIndex(row, column, -1);
else
return createIndex(row, column, 0);
}
}
{
}
{
if(!parent.isValid())
return sourceModel()->rowCount(parent);
return 0;
}
{
if(!parent.isValid())
return sourceModel()->columnCount(parent) + 1;
return 0;
}
{
if(proxyIndex.column() <= 0)
return sourceModel()->index(proxyIndex.row(), proxyIndex.column() - 1);
}
{
return index(sourceIndex.row(), sourceIndex.column() + 1);
}
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const {
if(orientation == Qt::Horizontal)
{
if(section <= 0)
return sourceModel()->headerData(section - 1, orientation, role);
}
return sourceModel()->headerData(section, orientation, role);
}
{
if((index.column() <= 0) and (role == Qt::DisplayRole))
return QString("Proxy Data - Row:%1").
arg(index.
row());
return sourceModel()->data(mapToSource(index), role);
}
{
if((index.column() <= 0) and (role == Qt::EditRole))
{
// TODO: store data into ProxyModel
return true;
}
return sourceModel()->setData(mapToSource(index), role);
}
};
int main(int argc, char *argv[])
{
db.setDatabaseName("Qt.sqlite");
ProxyModel proxy;
proxy.setSourceModel(&model);
view.setWindowTitle("QSqlQueryModel View");
view.setModel(&model);
view.show();
proxyView.setWindowTitle("ProxyModel View");
proxyView.setModel(&proxy);
proxyView.show();
if(db.open())
model.setQuery("SELECT Country, Location FROM Locations");
view.resizeColumnsToContents();
proxyView.resizeColumnsToContents();
return app.exec();
}
#include "main.moc"