Results 1 to 3 of 3

Thread: Strange things happening to QSqlTableModel (while inserting, deleting, updating)

  1. #1
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question Strange things happening to QSqlTableModel (while inserting, deleting, updating)

    Qt Code:
    1. model = new QSqlTableModel(this, db1);
    2. model->setTable("subjects");
    3. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    4. model->select();
    5. model->removeColumn(0); // don't show the ID
    6. ui->tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    Inserting:
    Qt Code:
    1. model->insertRow(0);
    To copy to clipboard, switch view to plain text mode 
    Deleting:
    Qt Code:
    1. QModelIndexList selection = ui->tableView->selectionModel()->selectedRows();
    2. if (selection.count())
    3. {
    4. for (int i = 0; i < selection.count(); i++)
    5. {
    6. QModelIndex index = selection.at(i);
    7. qDebug() << index.row();
    8. qDebug() << model->removeRow(selection.at(i).row());
    9. model->select();
    10. qDebug() << model->lastError();
    11. //model->submitAll();
    12. }
    13. //model1->submitAll();
    14. }
    To copy to clipboard, switch view to plain text mode 

    So, the table displays correctly.
    When I edit a cell, the row gets empty and shows !, like it was deleted. Nothing changes in DB.
    When I delete a row with algorithm above, removeRow returns true, the table view blinks like it's re-rendering, but nothing changes.
    When I insert a row, edit it, it gets empty and shows !, BUT it appears in DB and it's in the table when I reopen the app.
    When I un-comment model->submitAll(); nothing changes.
    Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Strange things happening to QSqlTableModel (while inserting, deleting, updating)

    The deletion code is definitely wrong.
    After the first modification all model indexes become invalid, so the loop continues with model indexes that are no longer appropriate.
    It also reloads the data before submitting the change, that looks problematic to me as well.

    Also the removal of a column looks strange. Did you want to use QTableView::setColumnHidden?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Strange things happening to QSqlTableModel (while inserting, deleting, updating)

    Quote Originally Posted by anda_skoa View Post
    Also the removal of a column looks strange. Did you want to use QTableView::setColumnHidden?
    It works now, thank you!
    I was 100% sure I took that part from the docs but now I see it's
    Qt Code:
    1. view->hideColumn(0); // don't show the ID
    To copy to clipboard, switch view to plain text mode 
    Anyway, thank you very much.


    Added after 33 minutes:


    I celebrated too soon, it fixed everything except:
    When I insert a row, edit it, it gets empty and shows !, BUT it appears in DB and it's in the table when I reopen the app.
    Last edited by Charlie42; 17th July 2015 at 12:49.

Similar Threads

  1. Replies: 0
    Last Post: 11th October 2012, 11:25
  2. Replies: 8
    Last Post: 30th March 2011, 21:06
  3. deleting record (row) from QSqlTableModel
    By schnitzel in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2010, 22:48
  4. Deleting QSqlTableModel and QTableView
    By waynew in forum Qt Programming
    Replies: 7
    Last Post: 24th December 2009, 01:17
  5. Treeview scrollbar not updating when inserting rows
    By Khal Drogo in forum Qt Programming
    Replies: 11
    Last Post: 29th November 2007, 14:13

Tags for this Thread

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.