QVariant data(const QModelIndex &index, int role) const { if(role == Qt::TextColorRole) { if(index.column() == 0) return QVariant(QColor(Qt::darkGray)); } if(role == Qt::TextAlignmentRole) { if(index.column() == 0) return QVariant(Qt::AlignRight | Qt::AlignVCenter); } return QSqlQueryModel::data(index, role); }
int row = model.rowCount(); model.insertRow(row); QModelIndex ind = model.index(row, 1); ui->tableView->setCurrentIndex(ind); ui->tableView->edit(ind);
//...model.setEditStrategy(QSqlTableModel::OnManualSubmit);//...//Add recordmodel.insertRows(model.rowCount(), 1);//...//Save recordmodel.submitAll();if( model.lastError().isValid() ) QMessageBox::warning(this,tr("Error"), model.lastError().text());