Results 1 to 8 of 8

Thread: delete row from QSqlTableModel in QTableView

  1. #1
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    10

    Default delete row from QSqlTableModel in QTableView

    Hello!

    I have a QSqlTableModel displayed in a QTableView.

    When I try to remove a row, the row does not disappear from the table view. There is just a "!" that appears on the side. I have tried with closePersistentEditor but it does not help.

    How is it possible to remove rows from the model?

    Thank you!

  2. #2
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: delete row from QSqlTableModel in QTableView

    qsqltablemodel::removeRows(...) should do the work you require. post some code you tried for.

  3. #3
    Join Date
    Mar 2010
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Re: delete row from QSqlTableModel in QTableView

    Hi! I have exactly same problem. Row remains visible in the view until submitAll execution, whitch is rather strange and confusing. At least because when you edit existant item, new value is shown until you ccommit or discard data.

    Here is slot, that I use to delete selected rows:

    Qt Code:
    1. def _removeSelectedStatuses(self):
    2. '''
    3. Удаляет выбранные строки из таблицы
    4.  
    5. pre[self]: self._model is not None
    6. '''
    7. model = self.ConservationStatusesTableView.selectionModel()
    8. l = model.selectedRows()
    9. if not len(l): return
    10.  
    11. rows = set([i.row() for i in l])
    12. rows = list(rows)
    13. rows.sort()
    14. first = rows[0]
    15. count = len(rows)
    16. self._model.removeRows(first, count)
    To copy to clipboard, switch view to plain text mode 

    If you know the answer, please, feel free to post it at
    http://stackoverflow.com/questions/4...-i-doing-wrong
    too.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: delete row from QSqlTableModel in QTableView

    QSqlTableModel caches changes until the submitAll() is called. The way it handles inserts and updates differs from the way it handles deletes. If you have a single model with two views then updates and inserts made in one model are immediately visible in the other view as you would expect. If you remove a row then the row remains in the views with the only thing identifying that the row is scheduled for deletion is the "!" in the header view. This behaviour is unique to the SQL models and does not exist in the in-memory models.

    To obtain the behaviour that users expect, i.e. the row disappears when deleted, the views have to either:
    • Hide the row based on the undocumented "!" marker. This has to happen in every view displaying the data every time a row is removed.
    • Force a submitAll() and commit all changes after delete. All views on the model then lose their selection and current item and you have to make efforts to maintain these (because that is what users expect). You have also lost the ability to revert pending changes.
    • Load the table entirely into an in-memory model. Whether this is acceptable is application dependent.


    I might have missed something and I'm all ears if someone has the elegant solution.
    Last edited by ChrisW67; 10th November 2010 at 23:18.

  5. The following user says thank you to ChrisW67 for this useful post:

    Al_ (18th March 2011)

  6. #5
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: delete row from QSqlTableModel in QTableView

    Quote Originally Posted by ChrisW67 View Post
    To obtain the behaviour that users expect, i.e. the row disappears when deleted, the views have to either:
    • Hide the row based on the undocumented "!" marker. ...
    • ...
    Does anybody know, how to detect the flag for deleted rows in a model? That is the flag corresponding to '!' marker in views. I want to strike-through these rows by returning a strike-through font in model::data(...) when asked for Qt::FontRole.

    It is not model->isDirty(index), as this returns true also for changed and not only for deleted cells.

    Al_

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: delete row from QSqlTableModel in QTableView

    Retrieve the vertical header for the row, it is literally "!"

  8. The following user says thank you to ChrisW67 for this useful post:

    Al_ (19th March 2011)

  9. #7
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default [SOLVED] delete row from QSqlTableModel in QTableView

    Thanks, works nicely. A bit unusual to look for a string representation as flag, but as long as it works ...

    Cheers

    Al_

  10. #8
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: [SOLVED] delete row from QSqlTableModel in QTableView

    I will keep here very good example (franku's post): http://qt-project.org/forums/viewthread/18581

    And this:
    Qt Code:
    1. void MainWindow::on_actionClear_triggered()
    2. {
    3. int rowCount = m_model->rowCount();
    4. m_model->removeRows( 0, rowCount );
    5. submit();
    6. }
    7.  
    8. void MainWindow::submit()
    9. {
    10. if( m_model->submitAll() ) {
    11. m_model->database().commit();
    12. } else {
    13. m_model->database().rollback();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSqlTableModel and QTableView critics.
    By Avrohom in forum Qt Programming
    Replies: 5
    Last Post: 11th September 2009, 03:17
  2. Replies: 2
    Last Post: 6th January 2009, 20:55
  3. QTableView and QSqlTableModel problem
    By Misenko in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2008, 21:58
  4. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 13:22
  5. QTableView and QSQLTableModel
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 4th March 2006, 18:09

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.