I'm struggling with getting two QDataWidgetMapper's that are tied to a single model to work properly.

This is the code I have:
Qt Code:
  1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
  2. {
  3. setupUi(this);
  4.  
  5. //Create the model
  6. _myModel = new QSqlTableModel(this);
  7. _myModel->setTable("mytable");
  8. _myModel->setEditStrategy(QSqlTableModel::OnRowChange);
  9. _myModel->select();
  10.  
  11. //Create the first mapper
  12. _myFirstMapper = new QDataWidgetMapper(this);
  13. _myFirstMapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  14. _myFirstMapper->setModel(_myModel);
  15. _myFirstMapper->addMapping(myFirstEdit, 0);
  16. connect(_myFirstMapper->itemDelegate(), SIGNAL(commitData(QWidget*)),
  17. this, SLOT(saveMyFirstData()));
  18.  
  19. //Create the second mapper
  20. _mySecondMapper = new QDataWidgetMapper(this);
  21. _mySecondMapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  22. _mySecondMapper->setModel(_myModel);
  23. _mySecondMapper->addMapping(mySecondEdit, 0);
  24. connect(_mySecondMapper->itemDelegate(), SIGNAL(commitData(QWidget*)),
  25. this, SLOT(saveMySecondData()));
  26.  
  27. //Jump to the same row for each mapper
  28. _myFirstMapper->toFirst();
  29. _mySecondMapper->toFirst();
  30. }
  31.  
  32. void saveMyFirstData() {
  33. int current = _myFirstMapper->currentIndex();
  34. _myFirstMapper->submit();
  35. _myFirstMapper->setCurrentIndex(current);
  36. }
  37.  
  38. void saveMySecondData() {
  39. int current = _mySecondMapper->currentIndex();
  40. _mySecondMapper->submit();
  41. _mySecondMapper->setCurrentIndex(current);
  42. }
To copy to clipboard, switch view to plain text mode 

Now, I connect the itemDelegate's signal to those slots because it was the only way I could use the QDataWidgetMapper with the ManualSubmit policy. The AutoSubmit policy did not work at all no matter what I tried.

What happens is when I change the value in "myFirstEdit" the second mapper gets it's currentIndex reset to -1. The reason is because the call to _myFirstMapper->submit() actually calls myModel->submit() which has the effect of resetting all QDataWidgetMappers attached to that model to -1. Why, I don't know.

Now the obvious correction to my code above is to have only one save slot like this:
Qt Code:
  1. void saveMyData() {
  2. int currentFirst = _myFirstMapper->currentIndex();
  3. int currentSecond = _mySecondMapper->currentIndex();
  4. _myFirstMapper->submit();
  5. _mySecondMapper->submit();
  6. _myFirstMapper->setCurrentIndex(currentFirst);
  7. _mySecondMapper->setCurrentIndex(currentSecond);
  8. }
To copy to clipboard, switch view to plain text mode 

Now here's my problem, what happens if the two different QDataWidgetMapper's are on separate forms that have no idea about each other, but they still use the same model. If I save data on the second form the first form's mapper's index gets set to -1.

Is this a bug in Qt or am I misunderstanding how to use QDataWidgetMapper?

And yes, I'd love for the AutoSubmit policy to work but it just plain doesn't work. At least not for me and the other people that had the same problem and suggested doing that signal slot connection.

Thanks, Mike