bool LogModel::setData(const QModelIndex &index, const QVariant &value, int role){ if (index.column() < 1) return false; QModelIndex primaryKeyIndex = QSqlQueryModel::index( index.row(), 0); int id = QSqlQueryModel::data(primaryKeyIndex).toInt(); bool ok; QSqlQuery query; if (index.column() > 1) { QString fieldName = getFieldNameByIndex(index.column()); query.prepare("UPDATE " + logName + " SET " + fieldName + " = ? WHERE KeyField = ?"); query.addBindValue(value.toString()); query.addBindValue(id); } ok = query.exec(); refresh(); // Проблема вот здесь return ok;}
bool LogModel::setData(const QModelIndex &index, const QVariant &value, int role){ if (index.column() < 1) return false; QModelIndex primaryKeyIndex = QSqlQueryModel::index( index.row(), 0); int id = QSqlQueryModel::data(primaryKeyIndex).toInt(); bool ok; QSqlQuery query; if (index.column() > 1) { QString fieldName = getFieldNameByIndex(index.column()); query.prepare("UPDATE " + logName + " SET " + fieldName + " = ? WHERE KeyField = ?"); query.addBindValue(value.toString()); query.addBindValue(id); } ok = query.exec(); QSqlQueryModel::setData(index, value); // В нашей модели меняем данные emit dataChanged(index, index); // Вот мы дали сигнал на изменение данных return ok;}
void LogTableView::slot_modelDataChange(const QModelIndex &topLeft, const QModelIndex &bottomRight){ qDebug() << "Row = " << topLeft.row() << " Col = " << bottomRight.column(); reset(); // Коммпанда перечитать данные из модели?}
QSqlQueryModel::setData(index, value);
QSqlQuery query; if (index.column() > 1) { QString fieldName = getFieldNameByIndex(index.column()); query.prepare("UPDATE " + logName + " SET " + fieldName + " = ? WHERE KeyField = ?"); query.addBindValue(value.toString()); query.addBindValue(id); } ok = query.exec();
bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value, int /* role */) { if (index.column() < 1 || index.column() > 2) return false; QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0); int id = data(primaryKeyIndex).toInt(); clear(); bool ok; if (index.column() == 1) { ok = setFirstName(id, value.toString()); } else { ok = setLastName(id, value.toString()); } refresh(); return ok; } void EditableSqlModel::refresh() { setQuery("select * from person"); setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); } bool EditableSqlModel::setFirstName(int personId, const QString &firstName) { QSqlQuery query; query.prepare("update person set firstname = ? where id = ?"); query.addBindValue(firstName); query.addBindValue(personId); return query.exec(); } bool EditableSqlModel::setLastName(int personId, const QString &lastName) { QSqlQuery query; query.prepare("update person set lastname = ? where id = ?"); query.addBindValue(lastName); query.addBindValue(personId); return query.exec(); }
void LogTableView::slot_modelDataChange(const QModelIndex &topLeft, const QModelIndex &bottomRight){ qDebug() << "Row = " << topLeft.row() << "Column = " << topLeft.column(); setUpdatesEnabled(false); QModelIndex i = model()->index(topLeft.row(), topLeft.column()); tableModel()->setQuery(tableModel()->query().lastQuery()); while(tableModel()->canFetchMore()) { tableModel()->fetchMore(); QCoreApplication::processEvents (); } setCurrentIndex(i); setUpdatesEnabled(true);}