Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. mapping data to a QComboBox
Forum Updated to NodeBB v4.3 + New Features

mapping data to a QComboBox

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you already check the Combo Box Widget Mapper Example ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by mzimmers
            #5

            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?

            1 Reply Last reply
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              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");
              
              1 Reply Last reply
              0
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                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...

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved