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. [Resolved] Assigning a display column to QComboBox
Qt 6.11 is out! See what's new in the release blog

[Resolved] Assigning a display column to QComboBox

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k Views 1 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.
  • O Offline
    O Offline
    onezeno
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      QComboBox' "modelColumn":http://qt-project.org/doc/qt-4.8/qcombobox.html#modelColumn-prop property

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onezeno
        wrote on last edited by
        #3

        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"));
        @

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onezeno
          wrote on last edited by
          #4

          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.

          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