Hi,

I have a SQL table model that maps RFID EPC codes to table indexes like this:

Qt Code:
  1. enum
  2. {
  3. RFID_ID = 0,
  4. RFID_EPC = 1,
  5. RFID_Name = 2,
  6. };
  7.  
  8. class RfidTagForm : public QDialog
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. RfidTagForm(int id, QWidget* parent = 0);
  14.  
  15. private slots:
  16. void addTag();
  17. void deleteTag();
  18.  
  19. private:
  20. QSqlTableModel* mTableModel;
  21.  
  22. QLabel* mEPCLabel;
  23. QLabel* mNameLabel;
  24. QLineEdit* mEPCEdit;
  25. QLineEdit* mNameEdit;
  26.  
  27. QPushButton* mFirstButton;
  28. QPushButton* mPreviousButton;
  29. QPushButton* mNextButton;
  30. QPushButton* mLastButton;
  31. QPushButton* mAddButton;
  32. QPushButton* mDeleteButton;
  33. [...]
  34. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. RfidTagForm::RfidTagForm(int id, QObject* parent) : QDialog(parent)
  2. {
  3. mEPCEdit = new QLineEdit;
  4.  
  5. [...]
  6. mTableModel = new QSqlTableModel(this);
  7. mTableModel->setTable("rfidtags");
  8. mTableModel->setSort(RFID_ID, Qt::AscendingOrder);
  9. mTableModel->select();
  10.  
  11. mMapper = new QDataWidgetMapper(this);
  12. mMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
  13. mMapper->setModel(mTableModel);
  14. mMapper->addMapping(mEPCEdit, RFID_EPC);
  15. mMapper->addMapping(mNameEdit, RFID_Name);
  16. [...]
  17.  
  18. if (id != -1)
  19. {
  20. for (int row = 0; row < mTableModel->rowCount(); ++row)
  21. {
  22. QSqlRecord record = mTableModel->record(row);
  23. if (record.value(RFID_ID).toInt() == id)
  24. {
  25. mMapper->setCurrentIndex(row);
  26. }
  27. }
  28. }
  29. else
  30. {
  31. mMapper->toFirst();
  32. }
  33.  
  34. connect(mFirstButton, SIGNAL(clicked()), mMapper, SLOT(toFirst()));
  35. connect(mPreviousButton, SIGNAL(clicked()), mMapper, SLOT(toPrevious)));
  36. connect(mNextButton, SIGNAL(clicked()), mMapper, SLOT(toNext()));
  37. connect(mLastButton, SIGNAL(clicked()), mMapper, SLOT(toLast()));
  38. connect(mAddButton, SIGNAL(clicked()), this, SLOT(addTag()));
  39. connect(mDeleteButton, SIGNAL(clicked()), this, SLOT(deleteTag()));
  40.  
  41. [...]
  42. // set up the UI stuff...
  43.  
  44. }
  45.  
  46.  
  47. void RfidTagForm::addTag()
  48. {
  49. int row = mMapper->currentIndex();
  50. mMapper->submit();
  51. mTableModel->insertRow(row);
  52. mMapper->setCurrentIndex(row);
  53.  
  54. mEPCEdit->clear();
  55. mNameEdit->clear();
  56. }
  57.  
  58. void RfidTagForm::deleteTag()
  59. {
  60. int row = mMapper->currentIndex();
  61. mTableModel->removeRow(row);
  62. mMapper->submit();
  63. mMapper->setCurrentIndex(qMin(row, mTableModel->rowCount() - 1));
  64. }
To copy to clipboard, switch view to plain text mode 

If the user now edits a new tag data and inserts a new RFID EPC Hex code (EPC is a long hex number to identify the tag) this should be converted to integer and taken as the record's ID for the table. But as far as I understand, QDataWidgetMapper sets the index automatically if it is set to AutoSubmit.

So is there any possibility to take influence on the data record ID when adding/deleting data records from the table? Or do I have to make this submit manual (ie set ManualSubmit and implement the slots for QDataWidgetMapper myself), but this does not solve my problem that I don't know the data record's index before the user has not edited the EPC line edit so that I can calculate the hex to integer record ID.

Thanks for any hint on this,
AlGaN