Results 1 to 4 of 4

Thread: deleting record (row) from QSqlTableModel

  1. #1
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default deleting record (row) from QSqlTableModel

    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.

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: deleting record (row) from QSqlTableModel

    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.

  3. The following user says thank you to waynew for this useful post:

    schnitzel (13th February 2010)

  4. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: deleting record (row) from QSqlTableModel

    Quote Originally Posted by waynew View Post
    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.

  5. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: deleting record (row) from QSqlTableModel

    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.

Similar Threads

  1. Deleting QSqlTableModel and QTableView
    By waynew in forum Qt Programming
    Replies: 7
    Last Post: 24th December 2009, 00:17
  2. Selected Record...
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 27th October 2009, 09:54
  3. Selected Record into TextEdit...
    By Nefastious in forum Newbie
    Replies: 4
    Last Post: 23rd October 2009, 17:57
  4. Inserting Record
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 04:28
  5. QTableView, add new record?
    By grellsworth in forum Qt Programming
    Replies: 6
    Last Post: 5th July 2007, 15:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.