QSqlTableModel updates the data when directly used but while subclassing setData does return true but updates nothing:
enum ItemRoles {
CONTENTROLE = Qt::UserRole + 1,
TYPEROLE = Qt::UserRole + 2,
VIEWSROLE = Qt::UserRole + 3,
WEIGHTROLE
};
QHash<int, QByteArray> ItemModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[CONTENTROLE] = "content";
roles[TYPEROLE] = "type";
roles[VIEWSROLE] = "views";
roles[WEIGHTROLE] = "weight";
return roles;
}
QVariant ItemModel::data(const QModelIndex & index, int role) const {
QModelIndex modelIndex = this->index(index.row(), role - Qt::UserRole);
return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
bool ItemModel::setData(const QModelIndex &index, const QVariant &value, int role) {
QModelIndex modelIndex = this->index(index.row(), role - Qt::UserRole);
qDebug() << QSqlTableModel::setData(modelIndex,value, Qt::EditRole); // true
qDebug() << lastError();
emit dataChanged(modelIndex,modelIndex);
return true;
}
//
QModelIndex index = itemModel->index(0, 0);
qDebug() << itemModel->data(index, itemModel->CONTENTROLE); // SOMETHING
itemModel->setData(index, "VALUE", itemModel->CONTENTROLE);
qDebug() << itemModel->data(index, itemModel->CONTENTROLE); // SOMETHING [should be VALUE]
Note: Most examples on the web are related to PyQt and there are very few references of c++ code in this regard.