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. QCombobox: Different column for showing and holding data

QCombobox: Different column for showing and holding data

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.8k Views
  • 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.
  • U Offline
    U Offline
    udx6bs
    wrote on last edited by
    #1

    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.

    Using Qt5.02/32bit on Windows Win7/64bit

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        udx6bs
        wrote on last edited by
        #3

        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 :)

        Using Qt5.02/32bit on Windows Win7/64bit

        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