Results 1 to 1 of 1

Thread: QSqlTableModel + QDataWidgetMapper - actual row changes into -1 after submit

  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QSqlTableModel + QDataWidgetMapper - actual row changes into -1 after submit

    Hi.

    I'm starting with Qt and have a problem - I want to create a form (created using QDataWidgetMapper) to edit records in SQLite database (listed in TableView), but every time changes are subbmited (I want them to be subbmited immediately after change, so I've set
    Qt Code:
    1. model->setEditStrategy(QSqlTableModel::OnFieldChange)
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit)
    To copy to clipboard, switch view to plain text mode 
    ) the acutal row changes into -1, last selected row is no longer selected and I must once again select it to continiue editing.

    How to prevent changing row to -1 after submit? or maybe any other solutions to make this editing easy, logic and working?
    I've read somwhere that's it's not bug, it's a feature but it makes my app useless.

    piece of my code (created using books example):
    (whole project zipped in attachements if somebody wants to experiment with it)
    Qt Code:
    1. #include "problem.h"
    2. #include "ui_problem.h"
    3.  
    4. #include <QtSql>
    5.  
    6. Problem::Problem(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Problem)
    9. {
    10. ui->setupUi(this);
    11. createDatabase();
    12.  
    13. model = new QSqlTableModel(ui->tableView);
    14. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    15. model->setTable("someTable");
    16.  
    17. model->select();
    18.  
    19. ui->tableView->setModel(model);
    20.  
    21. mapper = new QDataWidgetMapper(this);
    22. mapper->setModel(model);
    23. mapper->addMapping(ui->someLineEdit, model->fieldIndex("some"));
    24. mapper->addMapping(ui->thingLineEdit, model->fieldIndex("thing"));
    25. mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    26.  
    27. connect(ui->tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
    28. this, SLOT(rowChange(QModelIndex, QModelIndex)));
    29.  
    30. ui->tableView->setCurrentIndex(model->index(0, 0));
    31. }
    32.  
    33. Problem::~Problem()
    34. {
    35. delete ui;
    36. }
    37.  
    38. void Problem::createDatabase(){
    39. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    40. db.setDatabaseName(":memory:");
    41. if (!db.open()){
    42. QMessageBox::critical(this, "Error occured",
    43. "Error: " + db.lastError().text());
    44. }
    45.  
    46. q.exec("create table someTable(some varchar, thing varchar)");
    47.  
    48. q.prepare(QString("insert into someTable(some, thing) values(?, ?)"));
    49. for(int i = 0; i < 6; ++i){
    50. q.addBindValue(QString("some %1").arg(i));
    51. q.addBindValue(QString("thing %1").arg(6 - i));
    52. q.exec();
    53. }
    54. }
    55.  
    56. void Problem::rowChange(QModelIndex actual, QModelIndex last) {
    57. qDebug() << "row change - from" << last.row() << " to " << actual.row();
    58. if(actual.row() == -1)
    59. qDebug() << "wanted row: " << last.row();
    60. else
    61. qDebug() << "wanted row: " << actual.row();
    62. mapper->setCurrentModelIndex(actual);
    63. }
    To copy to clipboard, switch view to plain text mode 

    I've tried many solutions on my own already, but none of them worked.
    Attached Files Attached Files

Similar Threads

  1. Replies: 3
    Last Post: 4th October 2010, 03:57
  2. Replies: 1
    Last Post: 4th October 2010, 00:46
  3. QSqlTableModel slow even with manual submit
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2008, 20:40
  4. QODBC, QSqlTableModel, and submit problems
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2008, 12:46
  5. Replies: 3
    Last Post: 21st January 2008, 12:22

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.