This is how I define the model:
notesmodel->setTable("notes");
notesmodel->removeColumn(0); //don't show NoteID
notesmodel->removeColumn(0); //don't show nUnitID
notesmodel->select();
notesmodel = new QSqlTableModel;
notesmodel->setTable("notes");
notesmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
notesmodel->removeColumn(0); //don't show NoteID
notesmodel->removeColumn(0); //don't show nUnitID
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):
//printing out all notes for this unit
for(int i = 0; i< notesmodel->rowCount(); i++)
{
rpt.
append(QString("%1 %2").
arg(notesmodel
->record
(i
).
value(0).
toString()).
arg(notesmodel->record(i).value(1).toString()));
notesmodel
->removeRow
(i,
QModelIndex());
//doesn't work }
//printing out all notes for this unit
for(int i = 0; i< notesmodel->rowCount(); i++)
{
rpt.append(QString("%1 %2").arg(notesmodel->record(i).value(0).toString()).
arg(notesmodel->record(i).value(1).toString()));
notesmodel->removeRow(i, QModelIndex()); //doesn't work
}
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.
Bookmarks