hi,
i have subclassed QAbstractTableModel to make a model that works easily with tables that have a primary key. in the beginning i fill a QMap with the contents of the table like this:
Qt Code:
  1. void IndexedTableModel::setTable(QString mtable) {
  2. this->table = mtable;
  3. tableData.clear();
  4. // try to find an index:
  5. QSqlIndex idx = db.driver()->primaryIndex (table);
  6. Q_ASSERT(idx.count() == 1);
  7. indexColumn = idx.fieldName(0);
  8.  
  9. QString sql = "SELECT * FROM " + table;
  10. QSqlQuery query(sql);
  11. int row=0;
  12. while (query.next()) {
  13. QSqlRecord rec = query.record();
  14. int idxval = rec.field(indexColumn).value().toInt();
  15.  
  16. // as the rownumber can be unlike the value of the primary key
  17. // i store the information in a map:
  18. RowContent[row] = idxval;
  19.  
  20. tableData[row] = rec;
  21. row++;
  22. }
  23. }
To copy to clipboard, switch view to plain text mode 

as long as every cell in the table has a content everything works fine, i read the stuff into the model and can edit it. The problem happens when i come across a row that does not have any data apart from that in the primary key column. i had some debugging input at the end of the above function that assured me that tableData[row].count() is 3 as expected.
but when i try to edit one of the empty cells my function to find the QSqlField for this cell called from setData() bails out:

Qt Code:
  1. QSqlField IndexedTableModel::FieldToIndex(const QModelIndex &index) {
  2. int row = index.row();
  3. int col = index.column();
  4. int idx = RowContent[row];
  5. qDebug() << "searching for row " << row << " column " << col << " index " << idx;
  6.  
  7. if (!index.isValid()) {
  8. qDebug() << "..index was invalid ";
  9. return QSqlField();
  10. }
  11. QSqlRecord rec = tableData[idx];
  12. if (rec.count() <= col) {
  13. qDebug() << "..the record has only " << rec.count() << " columns " << " but you want column " << col;
  14. return QSqlField();
  15. } else {
  16. return rec.field(col);
  17. }
  18. }
To copy to clipboard, switch view to plain text mode 
will tell me that the record has 0 columns. Anybody knows why?