Results 1 to 9 of 9

Thread: Record and row in a QSqlTableModel

  1. #1
    Join Date
    Dec 2011
    Posts
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Record and row in a QSqlTableModel

    Hi all,
    This is my first question in this forum, so some hospitality please

    1/ What is the deference between record and row in a QSqlTableModel ?
    2/ How can I edit/remove a QSqlTableModel record/row ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Record and row in a QSqlTableModel

    Quote Originally Posted by QFreeCamellia View Post
    Hi all,
    This is my first question in this forum, so some hospitality please
    Then you might want to ask in the Newbie section. (->moved)

    1/ What is the deference between record and row in a QSqlTableModel ?
    Simplified there is no difference.
    2/ How can I edit/remove a QSqlTableModel record/row ?
    For editing see setData() and removeRows().

  3. #3
    Join Date
    Dec 2011
    Posts
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Updating a QSqlTableModel record

    Hi,

    When I insert a record in a not empty QSqlTableModel row, this row is temporally updated in the view, but in the database nothing happens.

  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: Updating a QSqlTableModel record

    QSqlTableModel has several strategies to govern if/when it commits changes to the underlying database. What is your editStrategy() ?

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Updating a QSqlTableModel record

    Hi, please use the original thread for questions related to the original problem! (merged)

  6. #6
    Join Date
    Dec 2011
    Posts
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating a QSqlTableModel record

    I tried all of them: OnFieldChange, OnManualSubmit and OnRowChange, but without effect

    Qt Code:
    1. QSqlRecord r = tableModel->record(5);
    2. r.setValue("name", name);
    3. r.setValue("age", age);
    4. tableModel->setRecord(5, r);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: Updating a QSqlTableModel record

    Ok, so you are using the QSqlTableModel specific interface and not the abstract model interface.

    If the edit strategy is OnFieldChange then the record is committed immediately (using submitAll() internally) otherwise a manual submitAll() is required to commit the changes to the underlying database.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName(":memory:");
    11. if (!db.open())
    12. qFatal("Database not open");
    13.  
    14. QSqlQuery query;
    15. query.exec("create table test (value varchar(100))");
    16. query.exec("insert into test values('Foo')");
    17.  
    18. model.setEditStrategy(QSqlTableModel::OnFieldChange);
    19. //model.setEditStrategy(QSqlTableModel::OnRowChange);
    20. //model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    21. model.setTable("test");
    22. model.select();
    23.  
    24. // Starting value
    25. QModelIndex index = model.index(0, 0);
    26. qDebug() << "Start" << index.data() ;
    27.  
    28. // Update the record outside the abstract model interface
    29. QSqlRecord rec = model.record(0);
    30. rec.setValue(0, QString("Bar"));
    31. bool ok = model.setRecord(0, rec);
    32. qDebug() << "setRecord OK" << ok;
    33.  
    34. // Let's see if the model is updated
    35. index = model.index(0, 0);
    36. qDebug() << "Update" << index.data();
    37.  
    38. // Explicit commit
    39. //model.submitAll();
    40.  
    41. // Let's see if we query directly
    42. QSqlQuery direct("select * from test");
    43. while (direct.next()) {
    44. qDebug() << "Committed" << direct.value(0);
    45. }
    46.  
    47.  
    48. return app.exec();
    49. }
    50. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. Start QVariant(QString, "Foo")
    2. setRecord OK true
    3. Update QVariant(QString, "Bar")
    4. Committed QVariant(QString, "Bar")
    To copy to clipboard, switch view to plain text mode 
    as-is. If you use either of the other two edit strategies then you need to also use submitAll();


    Qt 4.7.2 on Linux.

  8. #8
    Join Date
    Dec 2011
    Posts
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Updating a QSqlTableModel record

    Hi, can you check this code snippet please

    Qt Code:
    1. tableModel->setEditStrategy(QSqlTableModel::OnRowChange);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QSqlRecord r = tableModel->record(5);
    2.  
    3. qDebug() << "name: " << r.value("name");
    4. qDebug() << "age: " << r.value("age");
    5.  
    6. r.setValue("name", "newName");
    7. r.setValue("age", "newAge");
    8.  
    9. bool ok = tableModel->setRecord(5, r);
    10. qDebug() << "setRecord OK" << ok;
    11.  
    12. qDebug() << "name: " << r.value("name");
    13. qDebug() << "age: " << r.value("age");
    To copy to clipboard, switch view to plain text mode 

    The result:
    Qt Code:
    1. name: QVariant(QString, oldName)
    2. age: QVariant(QString, oldeAge)
    3.  
    4. QSqlQuery::value: not positioned on a valid record // I don't understand this
    5.  
    6. setRecord OK true
    7.  
    8. name: QVariant(QString, newName)
    9. age: QVariant(QString, newAge)
    To copy to clipboard, switch view to plain text mode 

    In the database nothing happens!

  9. #9
    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: Updating a QSqlTableModel record

    The reason nothing happens in the database is explained in my last post.

    Exactly which line triggers the warning message? Are there six rows in your table model? I cannot reproduce it with an example based on my code.

Similar Threads

  1. Error Modifiying a record of a QSqlTableModel
    By qlands in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2011, 11:41
  2. Replies: 7
    Last Post: 27th November 2010, 16:55
  3. Replies: 1
    Last Post: 4th October 2010, 01:46
  4. Replies: 3
    Last Post: 26th March 2010, 05:32
  5. deleting record (row) from QSqlTableModel
    By schnitzel in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2010, 22:48

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.