Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-1086

QDataWidgetMapper and QSqlTableModel problem

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Duplicate
    • Affects Version/s: 4.3.1
    • Fix Version/s: None
    • Component/s: SQL Support
    • Labels:
      None

      Description

      It seems that to get a QDataWidgetMapper to work with a QSqlTableModel one has make sure it calls toFirst() (or setCurrentIndex) whenever the model selects( ) (which will happen without the programmer explicitly doing it).

      Though this isn't too hard to do one could have expected this program to just work. Without uncommenting the emit selected() line only the first field is saved.

      It seems to be because the internal QPersistentModelIndexes that the QDataWidgetMapper uses are invalidated. The delegate's setModelData function will be invoked with an invalid QModelIndex.

      To reproduce:

      start app
      change the line edits to 123 and 456.
      quit the application
      restart the app
      notice that 123 was saved but not 456.

      #include <QtGui>
      
      class SqlTableModel : public QSqlTableModel
      {
          Q_OBJECT
      public:
          SqlTableModel(QWidget *parent = 0)
              : QSqlTableModel(parent)
          {}
      
          virtual bool select()
          {
              const bool ret = QSqlTableModel::select();
      //        emit selected(); // without this it doesn't work
              return ret;
          }
      signals:
          void selected();
      };
      
      class ItemDelegate : public QItemDelegate
      {
      public:
          ItemDelegate(QObject *parent = 0)
              : QItemDelegate(parent)
          {
          }
          virtual void setModelData(QWidget *editor,
                                    QAbstractItemModel *model,
                                    const QModelIndex &index) const
          {
              qDebug() << "setting" << editor->property("text").toString() << index;
              QItemDelegate::setModelData(editor, model, index);
          }
      };
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      public:
          MainWindow(QWidget *parent = 0)
              : QMainWindow(parent)
          {
              QWidget *w = new QWidget(this);
              setCentralWidget(w);
              QVBoxLayout *l = new QVBoxLayout(w);
              QLineEdit *edit1 = new QLineEdit(w);
              QLineEdit *edit2 = new QLineEdit(w);
              l->addWidget(edit1);
              l->addWidget(edit2);
      
              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              db.setDatabaseName("sample.db");
              if (!db.open()) {
                  Q_ASSERT(0);
              }
              QSqlQuery q(db);
              if (q.exec("CREATE TABLE fields (field1 VARCHAR(128), field2 VARCHAR(128));"))
                  q.exec("INSERT INTO fields (field1, field2) VALUES ('aaaaa', 'bbbbb')");
      
              SqlTableModel *model = new SqlTableModel(this);
              model->setTable("fields");
              model->select();
              model->setEditStrategy(QSqlTableModel::OnFieldChange);
      
              QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
              connect(model, SIGNAL(selected()), mapper, SLOT(toFirst()));
              mapper->setModel(model);
              mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
              mapper->setItemDelegate(new ItemDelegate(this));
      
              mapper->addMapping(edit1, model->fieldIndex("field1"));
              mapper->addMapping(edit2, model->fieldIndex("field2"));
              mapper->toFirst();
          }
      };
      
      #include "main.moc"
      
      int main(int argc, char **argv)
      {
          QApplication app(argc, argv);
          MainWindow mw;
          mw.show();
          return app.exec();
      }
      

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            mabrand Mark Brand
            Reporter:
            rve Anders Bakken
            Votes:
            8 Vote for this issue
            Watchers:
            7 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes