Results 1 to 2 of 2

Thread: QSqlDataWidgetMapper + QSqlTableModel +Sqlite = No Changes Written

  1. #1
    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

    Question QSqlDataWidgetMapper + QSqlTableModel +Sqlite = No Changes Written

    G'day,

    This could be a newbie mistake or a bug. Either way, I have been banging my head against this for a while. Help me work out which.

    I have a small test program that creates an Sqlite database on disk, puts some values in a table, and creates a simple form to edit these using QSqlDataWidgetMapper. The form navigation of the records works. I can edit a record, move away then back and the value has stayed on the model. Pressing Save on an edited row executes submit() on the mapper and prints the return (true) and lastError().text() (empty). AFAICT this should have written the changes to disk. However, If I change a record or two, Save, then exit and look at the content of the database it has not changed. What am I missing?

    Fails for me on Qt 4.5.1 Open source on Linux (Gentoo) or Qt 4.5.1 + MingW on Win XP using the all-in-one SDK download. Operative part of the code below, and whole program attached.

    Regards,
    Chris W

    Qt Code:
    1. #include "window.h"
    2.  
    3. Window::Window(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. setupModel();
    7.  
    8. nameEdit = new QLineEdit();
    9. nextButton = new QPushButton(tr("&Next"));
    10. previousButton = new QPushButton(tr("&Previous"));
    11. saveButton = new QPushButton(tr("&Save"));
    12.  
    13. mapper = new QDataWidgetMapper(this);
    14. mapper->setModel(model);
    15. mapper->addMapping(nameEdit, model->fieldIndex("name"));
    16.  
    17. connect(saveButton, SIGNAL(clicked()), this, SLOT(submitMe()));
    18. connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
    19. connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
    20. connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtons(int)));
    21.  
    22. QGridLayout *layout = new QGridLayout();
    23. layout->addWidget(nameEdit, 0, 0, 1, 1);
    24. layout->addWidget(previousButton, 0, 1, 1, 1);
    25. layout->addWidget(nextButton, 1, 1, 1, 1);
    26. layout->addWidget(saveButton, 2, 1, 1, 1);
    27. setLayout(layout);
    28.  
    29. setWindowTitle(tr("SQL Widget Mapper"));
    30. mapper->toFirst();
    31. }
    32.  
    33. void Window::setupModel()
    34. {
    35. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    36. db.setDatabaseName("test.db");
    37. if (!db.open()) {
    38. QMessageBox::critical(0, tr("Cannot open database"),
    39. tr("Unable to establish a database connection.\n"
    40. "This example needs SQLite support. Please read "
    41. "the Qt SQL driver documentation for information how "
    42. "to build it."), QMessageBox::Cancel);
    43. return;
    44. }
    45.  
    46. QSqlQuery query;
    47. query.exec("create table superhero (id int primary key, name varchar(20))");
    48. query.exec("insert into superhero values(1, 'Superman')");
    49. query.exec("insert into superhero values(2, 'Batman')");
    50. query.exec("insert into superhero values(3, 'Spiderman')");
    51. query.exec("insert into superhero values(4, 'Green Lantern')");
    52. query.exec("insert into superhero values(5, 'Phantom')");
    53.  
    54. model = new QSqlTableModel(this);
    55. model->setTable("superhero");
    56. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    57. model->select();
    58. }
    59.  
    60. void Window::updateButtons(int row)
    61. {
    62. previousButton->setEnabled(row > 0);
    63. nextButton->setEnabled(row < model->rowCount() - 1);
    64. }
    65.  
    66. void Window::submitMe()
    67. {
    68. bool ok = mapper->submit();
    69. qDebug() << "Window::submitMe()" << ok << model->lastError().text();
    70. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    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: QSqlDataWidgetMapper + QSqlTableModel +Sqlite = No Changes Written

    Newbie mistake.

    The underlying model is in EditStrategy::OnManualSubmit mode and the only way to commit to disc is the QSqlTableModel::submitAll() method. The mapper's submit() method only calls the model's submit() method, which does not commit to disc for models in manual mode. For my application I think EditStrategy::OnFieldChange will get me what I need although explicitly calling submitAll() would also work.

Similar Threads

  1. Replies: 2
    Last Post: 23rd February 2008, 01:58

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
  •  
Qt is a trademark of The Qt Company.