#include <QtGui>
#include <QtSql>
#include <QtWidgets>
#include <QApplication>
// Add one extra column at the begining
{
Q_OBJECT
public:
explicit ProxyModel
(QObject * parent
= 0) , mData()
{
connect(this, SIGNAL(sourceModelChanged()), SLOT(slotSourceModelChanged()));
}
protected slots:
void slotSourceModelChanged(void)
{
}
{
emit dataChanged(mapFromSource(first), mapFromSource(last));
}
{
if(!parent.isValid())
return createIndex(row, column);
}
{
}
{
if(!parent.isValid())
return 0;
}
{
if(!parent.isValid())
return sourceModel()->columnCount() + 1;
return 0;
}
{
if(!proxyIndex.isValid())
if(proxyIndex.column() == 0)
return createIndex(proxyIndex.row(), -1, (quintptr)-1); //<<<<<<<<<<<<<<<<<<<<<< This is a workaround. Note it is weird to create a index of proxy model and return as if it were an index of sourceModel(), it is not a common way but will work (at-least in this case)
return sourceModel()->index(proxyIndex.row(), proxyIndex.column() - 1);
}
{
if(!sourceIndex.isValid())
{
if((sourceIndex.row() > -1) and //<<<<<<<<<<<<<<<<<<<<<< This is a workaround.
(sourceIndex.column() == -1) and
(sourceIndex.internalId() == (quintptr)-1) )
return index(sourceIndex.row(), 0);
else
}
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)
{
if((role == Qt::DisplayRole) or (role == Qt::EditRole))
if(index.row() < mData.size())
return mData.at(index.row());
}
return sourceModel()->data(mapToSource(index), role);
}
{
if(index.column() == 0)
{
if((role == Qt::DisplayRole) or (role == Qt::EditRole))
{
while((index.row() + 1) > mData.size())
mData[index.row()] = value.toString();
emit dataChanged(index, index);
return true;
}
return false;
}
return sourceModel()->setData(mapToSource(index), value, role);
}
{
if(index.column() == 0)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
return sourceModel()->flags(mapToSource(index));
}
private:
};
{
public:
explicit Widget
(QWidget * parent
= 0) { }
void addWidget
(int row,
int col,
QWidget * widget,
const QString & title
) {
mGridLayout
->addWidget
(new QLabel(title
), row
* 2, col,
1,
1, Qt
::AlignCenter);
mGridLayout->addWidget(widget, (row * 2) + 1, col, 1, 1);
}
private:
};
const QString TableName
= "Locations";
const QString Column2
= "Location";
{
if(model->rowCount() == 0)
model->insertRows(0, countries.size());
if(model->columnCount() == 0)
model->insertColumns(0, 2);
for(int r = 0; r < model->rowCount(); r++)
{
model->setData(model->index(r, 0), countries.at(r));
model->setData(model->index(r, 1), locations.at(r));
}
model->setHeaderData(0, Qt::Horizontal, Column1);
model->setHeaderData(1, Qt::Horizontal, Column2);
model->submit();
}
{
tableView->setModel(model);
tableView
->setEditTriggers
(QTableView::DoubleClicked);
tableView->resizeColumnsToContents();
return tableView;
}
int main(int argc, char *argv[])
{
const QString fileName
= "Qt.sqlite";
if(dir.exists(fileName))
if(!dir.remove(fileName))
{
qDebug() << "Unable to delete old Database file " << fileName;
return -1;
}
sqlDatabase.setDatabaseName(fileName);
if(!sqlDatabase.open())
{
qDebug() << sqlDatabase.lastError().text();
return -2;
}
addSampleTableData(&standardItemModel);
if(!sqlQuery.
exec(QString("CREATE TABLE %1 ( %2 TEXT, %3 TEXT)").
arg(TableName
).
arg(Column1
).
arg(Column2
))) {
qDebug() << sqlQuery.lastError().text();
return -3;
}
sqlTableModel.setTable("Locations");
sqlTableModel.select();
addSampleTableData(&sqlTableModel);
sqlTableModel.submitAll();
sqlQueryModel.
setQuery(QString("SELECT %2, %3 FROM %1").
arg(TableName
).
arg(Column1
).
arg(Column2
), sqlDatabase
);
ProxyModel standardItemModelProxy;
standardItemModelProxy.setSourceModel(&standardItemModel);
ProxyModel sqlTableModelProxy;
sqlTableModelProxy.setSourceModel(&sqlTableModel);
ProxyModel sqlQueryModelProxy;
sqlQueryModelProxy.setSourceModel(&sqlQueryModel);
Widget widget;
widget.addWidget(0, 0, createView(&standardItemModel), "QStandardItemModel View");
widget.addWidget(0, 1, createView(&sqlTableModel), "QSqlTableModel View");
widget.addWidget(0, 2, createView(&sqlQueryModel), "QSqlQueryModel View");
widget.addWidget(1, 0, createView(&standardItemModelProxy), "QStandardItemModel Proxy View");
widget.addWidget(1, 1, createView(&sqlTableModelProxy), "QSqlTableModel Proxy View");
widget.addWidget(1, 2, createView(&sqlQueryModelProxy), "QSqlQueryModel Proxy View");
widget.showMaximized();
return app.exec();
}
#include "main.moc"