PDA

View Full Version : deleting record (row) from QSqlTableModel



schnitzel
11th February 2010, 08:59
This is how I define the model:


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();


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
}


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.

waynew
13th February 2010, 02:46
I'm not an expert, but I'll give it a try.
First of all, I would suggest not removing the NoteID and nUnitID columns from the model. Don't know your reason for doing this.
Instead, if you are using a view, use view->setColumnHidden(<column_number>, TRUE) that way the columns are still available in the model, but not shown.

As for the printing and removing, notesmodel->removeRow(i, QModelIndex()) looks like it would remove all rows.
I would need to see more of the surrounding code to see what is going on.
If you removed columns NoteID and nUnitID from the model, then column 0 is now Date.
You can't remove columns for a specific nUnitID after the column has been removed from the model.

schnitzel
13th February 2010, 21:32
I'm not an expert, but I'll give it a try.
First of all, I would suggest not removing the NoteID and nUnitID columns from the model. Don't know your reason for doing this.
Instead, if you are using a view, use view->setColumnHidden(<column_number>, TRUE) that way the columns are still available in the model, but not shown.

As for the printing and removing, notesmodel->removeRow(i, QModelIndex()) looks like it would remove all rows.
I would need to see more of the surrounding code to see what is going on.
If you removed columns NoteID and nUnitID from the model, then column 0 is now Date.
You can't remove columns for a specific nUnitID after the column has been removed from the model.

Thanks for your helpful post. For a non-expert you are pretty good!
I misunderstood the removing of columns and thought they would just be hidden. This will solve some of the problems for which I have implemented work arounds in other parts of my app.

waynew
13th February 2010, 22:48
You are welcome. I have misunderstood many Qt things and probably will again.
Hiding a column in the view, like the primary key id or such improves the looks but you can still use the column in the model, just not in the view, at least visually.

Good luck with your app.