The bounds between View and Model are the conventionalities... Qt4 provides for changing font, color, alignment in model, so it can be useful.
For NULLs there is Qt::ItemIsTristate.
setData needs to be reimplemented of course:
if (index.column() ......)
return false;
//clear();
bool ok;
if (index.column() == 1) {
query.prepare("update employee set name = ? where id = ?");
query.addBindValue(value.toString());
query.addBindValue(id);
.......
}else if(index.column() == aColWithCheck) {
query.prepare("update employee set married = ? where id = ?");
query.addBindValue(value.toInt());
query.addBindValue(id);
}
ok = query.exec();
refresh();
return ok;
}
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int /* role */) {
if (index.column() ......)
return false;
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = QSqlQueryModel::data(primaryKeyIndex).toInt();
//clear();
bool ok;
QSqlQuery query;
if (index.column() == 1) {
query.prepare("update employee set name = ? where id = ?");
query.addBindValue(value.toString());
query.addBindValue(id);
.......
}else if(index.column() == aColWithCheck) {
query.prepare("update employee set married = ? where id = ?");
query.addBindValue(value.toInt());
query.addBindValue(id);
}
ok = query.exec();
refresh();
return ok;
}
To copy to clipboard, switch view to plain text mode
ItemIsUserCheckable in flags() and CheckStateRole in data() achieve the interesting feature: checkbox is clicked -> text label is changed in cell.
Proxy model 'Bool to CheckStateRole' is realy the best i think
What about 'Qt Cookbook'?
Bookmarks