This is how I define the model:
Qt Code:
  1. notesmodel = new QSqlTableModel;
  2. notesmodel->setTable("notes");
  3.  
  4. notesmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
  5. notesmodel->removeColumn(0); //don't show NoteID
  6. notesmodel->removeColumn(0); //don't show nUnitID
  7. notesmodel->select();
To copy to clipboard, switch view to plain text mode 

The 'notes' table contains the following fields:
NoteID
nUnitID
Date
Note

At some point I apply a filter to only show the notes for a specific nUnitID. This all works fine.

I run into problems when I try to delete all notes for a specific nUnitID after printing them out with the following code (notesmodel is still filtered at this point):
Qt Code:
  1. //printing out all notes for this unit
  2. for(int i = 0; i< notesmodel->rowCount(); i++)
  3. {
  4. rpt.append(QString("%1 %2").arg(notesmodel->record(i).value(0).toString()).
  5. arg(notesmodel->record(i).value(1).toString()));
  6. notesmodel->removeRow(i, QModelIndex()); //doesn't work
  7. }
To copy to clipboard, switch view to plain text mode 

When I run the above code, I get an error message:
QSqlQuery::value: not positioned on a valid record

I feel it has something to do with not getting a correct model index.

Alternatively I could just use a QSqlQuery and use the DELETE syntax, but I thought using the model would be simpler.