#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"
#include <QtGui>
#include <QtSql>
#include <QApplication>
// Add one extra column at the beginning
class ProxyModel : public QAbstractProxyModel
{
Q_OBJECT
public:
explicit ProxyModel(QObject * parent = 0)
: QAbstractProxyModel(parent)
{
;
}
void setSourceModel(QAbstractItemModel *sourceModel)
{
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()));
disconnect(this->sourceModel(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)));
}
QAbstractProxyModel::setSourceModel(sourceModel);
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()));
connect(this->sourceModel(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)));
}
}
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
{
if(!parent.isValid())
{
if(column == 0)
return createIndex(row, column, -1);
else
return createIndex(row, column, 0);
}
return QModelIndex();
}
QModelIndex parent(const QModelIndex & /*child*/) const
{
return QModelIndex();
}
int rowCount(const QModelIndex & parent = QModelIndex()) const
{
if(!parent.isValid())
return sourceModel()->rowCount(parent);
return 0;
}
int columnCount(const QModelIndex & parent = QModelIndex()) const
{
if(!parent.isValid())
return sourceModel()->columnCount(parent) + 1;
return 0;
}
QModelIndex mapToSource(const QModelIndex & proxyIndex) const
{
if(proxyIndex.column() <= 0)
return QModelIndex();
return sourceModel()->index(proxyIndex.row(), proxyIndex.column() - 1);
}
QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
{
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 QString("Proxy Column");
return sourceModel()->headerData(section - 1, orientation, role);
}
return sourceModel()->headerData(section, orientation, role);
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if((index.column() <= 0) and (role == Qt::DisplayRole))
return QString("Proxy Data - Row:%1").arg(index.row());
return sourceModel()->data(mapToSource(index), role);
}
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
{
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[])
{
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("Qt.sqlite");
QSqlQueryModel model;
ProxyModel proxy;
proxy.setSourceModel(&model);
QTableView view;
view.setWindowTitle("QSqlQueryModel View");
view.setModel(&model);
view.show();
QTableView proxyView;
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"
To copy to clipboard, switch view to plain text mode
Bookmarks