C++ (Qt)bool LogModel::setData(const QModelIndex &index, const QVariant &value, int role){ QSqlTableModel::setData(index, value, role); if (index.column() < 1) return false; return updateDataForColumn(getFieldNameByIndex(index.column()), index, value);} bool LogModel::updateDataForColumn(QString columnName, const QModelIndex &index, const QVariant &value){ QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0); int id = QSqlQueryModel::data(primaryKeyIndex).toInt(); QSqlQuery query; if (index.column() > 1) { query.prepare("UPDATE " + globalVariable->logName + " SET " + columnName + " = ? WHERE KeyField = ?"); query.addBindValue(value.toString()); query.addBindValue(id); } bool b = query.exec(); emit dataChanged(index, index); return b;}
C++ (Qt)bool LogModel::setData(const QModelIndex &index, const QVariant &value, int role){ QSqlTableModel::setData(index, value, role); if (index.column() < 1) return false; return updateDataForColumn(getFieldNameByIndex(index.column()), index, value);}