Results 1 to 1 of 1

Thread: How to delete a row from the model without making any changes to the database?

  1. #1
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to delete a row from the model without making any changes to the database?

    Hello!
    Added to the model line (which have not yet saved) are normally removed. And those who were already in the database, it seems not removed but only marked for deletion, and self-deletion occurs after the call submitAll(). Source code examples in attachment.

    Is there any way to delete rows from the model without invoking submitAll()?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtSql>
    3.  
    4. bool createConnection()
    5. {
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    7. db.setDatabaseName("test.sqlite");
    8. if (!db.open())
    9. return false;
    10. return true;
    11. }
    12.  
    13. // Create a table and populate it with test data
    14. void createFakeData()
    15. {
    16. QSqlQuery query;
    17.  
    18. query.exec("DROP TABLE users");
    19.  
    20. query.exec("CREATE TABLE users ("
    21. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    22. "name VARCHAR(40) NOT NULL DEFAULT '')");
    23.  
    24. query.exec("INSERT INTO users (name) VALUES ('john')");
    25. query.exec("INSERT INTO users (name) VALUES ('bill')");
    26. }
    27.  
    28. int main(int argc, char *argv[])
    29. {
    30. QCoreApplication a(argc, argv);
    31.  
    32. if (!createConnection())
    33. return 1;
    34. createFakeData();
    35.  
    36. model.setTable("users");
    37. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    38. model.select();
    39.  
    40. qDebug() << "rowCount:" << model.rowCount(); // rowCount: 2
    41.  
    42. qDebug() << "insertRow()";
    43. int row = model.rowCount();
    44. model.insertRow(row);
    45. qDebug() << "rowCount:" << model.rowCount(); // rowCount: 3
    46.  
    47. qDebug() << "removeRow()";
    48. model.removeRow(row);
    49. qDebug() << "rowCount:" << model.rowCount(); // rowCount: 2
    50.  
    51. qDebug() << "removeRow()";
    52. model.removeRow(row-1);
    53. qDebug() << "! rowCount:" << model.rowCount(); // rowCount: 2 (!)
    54.  
    55. qDebug() << "\nsubmitAll()";
    56. model.submitAll();
    57. qDebug() << "! rowCount:" << model.rowCount(); // rowCount: 1
    58.  
    59. return a.exec();
    60. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

Similar Threads

  1. Making a tree proxy model with multiple sources
    By Mookie in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2010, 03:28
  2. Replies: 2
    Last Post: 17th February 2010, 14:32
  3. trouble making simple DOM model example editable
    By Mamra in forum Qt Programming
    Replies: 6
    Last Post: 1st October 2009, 14:40
  4. using QTreeView with a Database model
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2007, 23:29
  5. how to use QTreeView with Database model
    By mikro in forum Newbie
    Replies: 3
    Last Post: 13th April 2006, 16:12

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.