[Resolved] Assigning a display column to QComboBox



  • I'd like to setup my ComboBox backed with an SQLTableModel so that the value is mapped to an ID column, but the displayed text comes from a NAME column. I can't use the result from "this example":http://www.trinitydesktop.org/docs/qt4/itemviews-combowidgetmapper.html because the ID is not necessarily the same as the currentIndex. Is there a solution for this (which seems like a common use case) or will I need to subclass QComboBox to get the desired functionality?


  • Moderators



  • I use modelColumn already. Let's say I have a table Dog, with fields id, name, and breed, where breed is a foreign key to table Breed, with columns id and name. So my combo box looks something like this:

    @
    myComboBox->setModel(dogModel);
    myCombBox->setModelColumn(2);
    @

    When I open an editor form, and use a QDataWidgetMapper, the comboBox is populated with the appropriate numerical values, and the correct one is selected. When I make changes, they are properly persisted in the database. However, I don't want to display the breed id; I want to display the name of the breed. But I want everything else to work as is.

    EDIT:
    Also, if it helps, I've defined a relationship already in the QSqlRelationalTableModel.

    @
    dogModel->setRelation(2, QSqlRelation("Breed", "id", "name"));
    @



  • Okay, resolved, there was an issue with my QRelationalTableModel I was setting the relation before the table, so basically the foreign-key relationship was not acknowledged.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.