Hi, I'm new to Qt (using QT 6.1 on Windows 10).

I use QSqlRelationalTableModel to show database table ("payments") with foreign keys to other table ("students"). This works fine.
I created a form to edit each table row using a QDataWidgetMapper.
All the ui Widgets for edition (LineEdits and one ComboBox) are created with Qt Designer. This is the relevant code:

Qt Code:
  1. {
  2.  
  3. m_paymentsTableModel = getPaymentsModel(); // m_paymentsTableModel is QSqlRelationalTableModel*
  4. m_paymentsTableModel->setRelation(1, QSqlRelation("students", "studentId", "surname"));
  5.  
  6. m_relatedModel = m_paymentsTableModel->relationModel(1); // m_relatedModel is from table "students", Column 1 is "studentId" in table "payments".
  7. ui->studentComboBox->setModel(m_relatedModel);
  8. ui->studentComboBox->setModelColumn(2); //column 2 is "surname" in table "students".
  9.  
  10. m_paymentsMapper = new QDataWidgetMapper(this);
  11. m_paymentsMapper->setModel(m_paymentsTableModel);
  12.  
  13. m_paymentsMapper->addMapping(ui->paymentNumberLineEdit, 0);
  14. m_paymentsMapper->addMapping(ui->studentComboBox, 1);
  15. m_paymentsMapper->addMapping(ui->startDateLineEdit, 2);
  16. m_paymentsMapper->addMapping(ui->endDateLineEdit, 3);
  17. m_paymentsMapper->addMapping(ui->allowedLessonsLineEdit, 4);
  18. m_paymentsMapper->addMapping(ui->remainingLessonsLineEdit, 5);
  19. m_paymentsMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
  20. m_paymentsMapper->setItemDelegate(new QSqlRelationalDelegate(m_paymentsMapper));
  21. m_paymentsMapper->toFirst();
  22.  
  23. QObject::connect(ui->paymentsFirstButton, &QPushButton::clicked, m_paymentsMapper,
  24. &QDataWidgetMapper::toFirst);
  25. QObject::connect(ui->paymentsNextButton, &QPushButton::clicked, m_paymentsMapper,
  26. &QDataWidgetMapper::toNext);
  27. QObject::connect(ui->paymentsPreviousButton, &QPushButton::clicked, m_paymentsMapper,
  28. &QDataWidgetMapper::toPrevious);
  29. QObject::connect(ui->paymentsLastButton, &QPushButton::clicked, m_paymentsMapper,
  30. &QDataWidgetMapper::toLast);
  31. QObject::connect(ui->paymentsAddButton, &QPushButton::clicked, this,
  32. &AbonosWidget::addPayment);
  33. QObject::connect(ui->paymentsDeleteButton, &QPushButton::clicked, this,
  34. &AbonosWidget::deletePayment);
  35. QObject::connect(m_paymentsMapper, &QDataWidgetMapper::currentIndexChanged, ui->studentComboBox,
  36. &QComboBox::setCurrentIndex);
  37.  
  38. }
To copy to clipboard, switch view to plain text mode 

My problem is with the ComboBox for choosing the correct "studentId".
ComboBox has his own Model (from table "students") and shows "surname" instead of "studentId". That's fine.

But the ComboBox follows his own order, seems to be detached from mapper because doesn't show right "surname" according to "paymentId".

I followed Qt examples (Combo Widget Mapper Example) and Documentation, and according suggestions finally attached a QSqlRelationalDelegate() to the mapper but nothing happens...

Any suggestions?

I would appreciate your help very much. Thanks in advance!