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::insertRows(int position, int rows, const QModelIndex &index){ Q_UNUSED(index); beginInsertRows(QModelIndex(), position, position + rows - 1); QSqlQuery query; query.prepare("INSERT INTO " + globalVariable->logName + " (Name) VALUES (:Name)"); query.bindValue(":Name", ""); for (int row = 0; row < rows; row++) { query.exec(); } endInsertRows(); return true;}
if(index.column()>=1) return Qt::ItemIsEditable;
C++ (Qt)Qt::ItemFlags LogModel::flags(const QModelIndex &index) const{ Qt::ItemFlags flags = QSqlQueryModel::flags(index); if(isBoolField(index.column())) flags |= Qt::ItemIsUserCheckable; else if (index.column() >= 1) flags |= Qt::ItemIsEditable; return flags;}
C++ (Qt)setEditStrategy(QSqlTableModel::OnManualSubmit);
C++ (Qt) QSqlRecord record = logModel->record(); record.setValue(... добавили все что надо); logModel->insertRecord(-1, record); logModel->submitAll();
C++ (Qt) if( !logModel->insertRecord(-1,record) ) { qDebug() << logModel->lastError().text(); logModel->revertAll(); }
C++ (Qt)if( !logModel->submitAll()){ qDebug() << logModel->lastError().text(); logModel->revertAll();}
C++ (Qt)if( !logModel->submitAll()){ qDebug() << logModel->lastError().text(); qDebug() << logModel->query().lastQuery(); logModel->revertAll();}
C++ (Qt)qobject_cast<QSqlQueryModel*>(this)->setQuery(queryStr);
C++ (Qt) while(canFetchMore()) { fetchMore(); QCoreApplication::processEvents(); }
C++ (Qt)QString SqlTableModel::orderByClause() const{ QString s = QSqlTableModel::orderByClause(); if(!s.isNull()) s.append(QLatin1String(" NULLS FIRST" )); return s;}
C++ (Qt)bool QSqlTableModel::submitAll(){ Q_D(QSqlTableModel);............................ d->clearCache(); return select(); } return false;}