QCombobox: Different column for showing and holding data



  • Hello there,
    this is probably a basic problem, but i couldn't google it anywhere:(

    I have a QDataWidgetMapper connected to a QComboBox.
    QComboBox's model has 2 columns:
    [0] -> Column which has the primary keys.
    [1] -> Column which has the the data to show.

    The model which QDataWidgetMapper uses has only the Primary key value.

    Is it possible to pass column [0] values to QComboBox and it sets the associated column [1] data to show in its dropdown.

    My english is not the best hope its understandable.

    Thx in advance.



  • For QComboBox we can use "addItem()":http://qt-project.org/doc/qt-5.0/qtwidgets/qcombobox.html#addItem where we can also pass a QVariant to it. For eg

    @m_actionCombo->addItem(column[1]_data, QVariant::fromValue(column[0]));@

    Hope it helps.



  • Well, not quite what i'm looking for.

    I've had a lot of attempts (changing QComboBoxs view, Experimtenting with QRelational, ...) without success and went to sleep. Now i got up and it hit me.

    Now it looks like this:

    @
    // mapper to the comboBox:
    ...
    m_mapper.addMapping(m_expansion.comboBox, CModels::CardItemExpansion, "currentValue");
    ...

    //slot called when the main models currentIndex changes:
    void CEdit::setIndex(int index) {
    m_mapper.setCurrentIndex(index);

    int rowCount = m_expansion.comboBox->model()->rowCount();
    QVariant value = m_expansion.comboBox->property("currentValue");
    for (int i = 0; i < rowCount; ++i) {
    if (m_expansion.comboBox->model()->index(i, CModels::ExpansionId).data() == value) {
    m_expansion.comboBox->setCurrentIndex(i);
    break;
    }
    }
    }
    @

    Thx anyway
    If u have a more elegant solution i'm all ears :)


Log in to reply
 

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