PDA

View Full Version : QComboBox - QDataWidgetMapper - QSqlRelationalTableModel



edu53
14th June 2021, 14:07
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:




{

m_paymentsTableModel = getPaymentsModel(); // m_paymentsTableModel is QSqlRelationalTableModel*
m_paymentsTableModel->setRelation(1, QSqlRelation("students", "studentId", "surname"));

m_relatedModel = m_paymentsTableModel->relationModel(1); // m_relatedModel is from table "students", Column 1 is "studentId" in table "payments".
ui->studentComboBox->setModel(m_relatedModel);
ui->studentComboBox->setModelColumn(2); //column 2 is "surname" in table "students".

m_paymentsMapper = new QDataWidgetMapper(this);
m_paymentsMapper->setModel(m_paymentsTableModel);

m_paymentsMapper->addMapping(ui->paymentNumberLineEdit, 0);
m_paymentsMapper->addMapping(ui->studentComboBox, 1);
m_paymentsMapper->addMapping(ui->startDateLineEdit, 2);
m_paymentsMapper->addMapping(ui->endDateLineEdit, 3);
m_paymentsMapper->addMapping(ui->allowedLessonsLineEdit, 4);
m_paymentsMapper->addMapping(ui->remainingLessonsLineEdit, 5);
m_paymentsMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
m_paymentsMapper->setItemDelegate(new QSqlRelationalDelegate(m_paymentsMapper));
m_paymentsMapper->toFirst();

QObject::connect(ui->paymentsFirstButton, &QPushButton::clicked, m_paymentsMapper,
&QDataWidgetMapper::toFirst);
QObject::connect(ui->paymentsNextButton, &QPushButton::clicked, m_paymentsMapper,
&QDataWidgetMapper::toNext);
QObject::connect(ui->paymentsPreviousButton, &QPushButton::clicked, m_paymentsMapper,
&QDataWidgetMapper::toPrevious);
QObject::connect(ui->paymentsLastButton, &QPushButton::clicked, m_paymentsMapper,
&QDataWidgetMapper::toLast);
QObject::connect(ui->paymentsAddButton, &QPushButton::clicked, this,
&AbonosWidget::addPayment);
QObject::connect(ui->paymentsDeleteButton, &QPushButton::clicked, this,
&AbonosWidget::deletePayment);
QObject::connect(m_paymentsMapper, &QDataWidgetMapper::currentIndexChanged, ui->studentComboBox,
&QComboBox::setCurrentIndex);

}


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!

edu53
20th June 2021, 00:23
I found myself the solution :).

My function getPaymentsModel() made a select(), so
model->select() went before settting the relations.

The order of actions is (as correctly stated in Qt docs examples):

1) model-> setTable()
2) model->setEditStrategy()
3) model->setRelation()
4) model->setHeaderData()
5)model->select()