Results 1 to 5 of 5

Thread: Populating a combo box from another relation

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Populating a combo box from another relation

    Hello all,

    I am trying to populate two combo boxes from another relation using the code below:

    Qt Code:
    1. // Setup the model to use in the mapper
    2. model = new QSqlRelationalTableModel(this);
    3. model->setTable("Orders");
    4. model->setRelation(7, QSqlRelation("customers", "CustomerNumber", "Name"));
    5. model->setRelation(5, QSqlRelation("employee", "EmployeeNumber", "UserName"));
    6. model->select();
    7. Qt::SortOrder order = Qt::AscendingOrder;
    8. model->sort(0, order);
    9.  
    10. // Setup the mapper for the order widgets
    11. mapper = new QDataWidgetMapper(this);
    12. mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    13. mapper->setModel(model);
    14. mapper->setItemDelegate(new QSqlRelationalDelegate(model));
    15. mapper->addMapping(ui->idLineEdit, 0);
    16. mapper->addMapping(ui->fullfilledCheckBox,1);
    17. mapper->addMapping(ui->fullfilledDateEdit, 2);
    18. mapper->addMapping(ui->orderDateEdit, 3);
    19. mapper->addMapping(ui->dueDateEdit, 4);
    20. mapper->addMapping(ui->employeeComboBox, 5, "currentIndex");
    21. mapper->addMapping(ui->dueTimeEdit, 6);
    22. mapper->addMapping(ui->customerComboBox, 7, "currentIndex");
    To copy to clipboard, switch view to plain text mode 

    The attribute names and numbers are correct. The orders relation has employee number in column 5 and customer number in column 7. The code runs and shows the order mapped correctly but the combo boxes are empty.

    Any idea why this is happening?

    Thanks,

    Pericles

  2. #2
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Populating a combo box from another relation

    I managed to add the names in the qcombobox using the following code:
    Qt Code:
    1. ui->customerComboBox->setModel(model->relationModel(7));
    2. ui->customerComboBox->setModelColumn(1);
    To copy to clipboard, switch view to plain text mode 
    But now when I save I save the index of the combo box instead of the actual value that relates to the name shown. Anyone knows how I can save the Customer Number (stored in column 0) in the customer table rather than the combobox index when I submit the model?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Populating a combo box from another relation

    Shouldn't the comboboxes also have the relational delegate applied?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Populating a combo box from another relation

    I decided to keep it simple now until I figure out how to do the combo boxes. I used a LineEdit and it works fine. The only difference is that I do not have a dropdown but I had to implement a Find function next to it that inserts the value.

    Wysota: How should I apply the relational delegate to the combobox?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Populating a combo box from another relation

    Quote Originally Posted by pcheng View Post
    Wysota: How should I apply the relational delegate to the combobox?
    QComboBox::setItemDelegate()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt Parent <-> Child Relation When Painting
    By axed in forum Qt Programming
    Replies: 9
    Last Post: 20th April 2012, 16:08
  2. Multiple axes with a relation
    By pkj in forum Qwt
    Replies: 1
    Last Post: 21st February 2011, 06:46
  3. Populating, signals and comboboxes
    By ShamusVW in forum Newbie
    Replies: 6
    Last Post: 12th August 2010, 06:43
  4. QComboBox QSqlQueryModel & relation.
    By matheww in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 04:56
  5. Is there a relation between Qt and Quick Time?
    By Mariane in forum General Discussion
    Replies: 18
    Last Post: 24th January 2006, 21:23

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.