Results 1 to 2 of 2

Thread: QComboBox - QDataWidgetMapper - QSqlRelationalTableModel

  1. #1
    Join Date
    Jun 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Post QComboBox - QDataWidgetMapper - QSqlRelationalTableModel

    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!

  2. #2
    Join Date
    Jun 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox - QDataWidgetMapper - QSqlRelationalTableModel

    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()

Similar Threads

  1. Replies: 2
    Last Post: 29th December 2012, 15:14
  2. Replies: 4
    Last Post: 13th June 2010, 13:31
  3. QSqlRelationalTableModel QDataWidgetMapper
    By kawoN in forum General Programming
    Replies: 4
    Last Post: 26th May 2010, 10:30
  4. QDataWidgetMapper and QComboBox
    By mazurekwrc in forum Qt Programming
    Replies: 0
    Last Post: 31st March 2009, 14:02
  5. QDataWidgetMapper and QSqlRelationalTableModel problem
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 16:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.