mapping data to a QComboBox



  • Hi all -

    I'm playing with model/view stuff, and learning (slowly) as I go. I'm trying to map a data column from a QStandardItemModel to a QComboBox.

    model.cpp

    ...
        qs = d.ledPattern;
        rc = m_model->setData(m_model->index(row, MODEL_COL_LED_PATTERN), qs);
    

    and widget.cpp

    const string ledPatternText[LEDC_NBR_PATTERNS] =
    {
        "SOLID_OFF",
        "SOLID_ON",
        "FLASH_SLOW",
        "FLASH_RAPID"
    };
    ...
        for (int i = 0; i < LEDC_NBR_PATTERNS; ++i)
        {
            qs = QString::fromStdString(ledPatternText[i]);
            ui->comboBoxLedPattern->addItem(qs);
        }
        mapper->addMapping(ui->comboBoxLedPattern, MODEL_COL_LED_PATTERN);
    

    The combo box doesn't seem mapped to the data. It always displays "SOLID_OFF." Can someone see what I'm doing wrong here?


  • Lifetime Qt Champion

    Hi,

    Did you already check the Combo Box Widget Mapper Example ?



  • I did now.

    So...model.cpp:

        QString qs;
        QStringList qsl;
      ...
        for (int i = 0; i < LEDC_NBR_PATTERNS; ++i)
        {
            qs = QString::fromStdString(ledPatternText[i]);
            qsl << qs;
        }
        m_ledPatternModel = new QStringListModel(qsl, this);
    

    and widget.cpp:

        ui->comboBoxLedPattern->setModel(d->getLedPatternModel());
        mapper->addMapping(ui->comboBoxLedPattern, MODEL_COL_LED_PATTERN, "currentIndex");
    

    I'm still missing something, though...no change in program behavior.



  • OK, I think I have it working now. Here's the program flow.

    1. the worker object receives a message from a connected device, and converts the message from XML into a C data structure. The value for the LED pattern combo box is stored as an int from 0 to 3 within this struct:
    DeviceDetails d;
    string s;
    ...
     s = msg.getValue(msgTags[TAGENUM_LEDPATTERN]);
        d.ledPattern = ledPatternHash.at(s);
    

    A signal is sent to the model.

    1. the model sets the value onto the proper column:
        rc = m_model->setData(m_model->index(row, MODEL_COL_LED_PATTERN), d.ledPattern);
    

    This is the step I was missing.

    1. the display widget updates the combo box that represents this value because of this code:
        ui->comboBoxLedPattern->setModel(d->getLedPatternModel());
        mapper->addMapping(ui->comboBoxLedPattern, MODEL_COL_LED_PATTERN, "currentIndex");
    

    The examples are excellent, but can be a little confusing because, in the interest of brevity, they embed the model in the widget code. It took me a little time to find this missing piece.



  • One remaining issue: the display doesn't update to reflect a remote change to the combo box, until the user does something (seemingly anything).

    I think I've run into this before, a long time ago, but I don't remember what the fix is.

    EDIT:

    BTT. Anyone have any ideas?



  • I just discovered an issue-- while my QTableView properly displays multiple entries, my mapping to the combo box is only displaying one entry (the first one entered into the model). If I select a different entry from the table, the combo box is updated, but it doesn't display anything other than the selected entry.

    Do I need to do something besides the standard mapping?

        mapper = new QDataWidgetMapper(this);
        mapper->setModel(d->getModel());
       ...
        mapper->addMapping(ui->comboBoxMacAddr, MODEL_COL_MACADDR);
        mapper->addMapping(ui->comboBoxLedPattern, MODEL_COL_LED_PATTERN, "currentIndex");
    


  • I never got this fixed, but I decided that having two places to select the MAC address was a bad idea anyway, so I replaced the QComboBox with a QLineEdit. Added this line to my main widget:

        mapper->addMapping(ui->lineEditMacAddr, MODEL_COL_MACADDR);
    

    And it seems to work fine. Marking as solved...


Log in to reply
 

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