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. [SOLVED] QComboBox with multiple columns
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QComboBox with multiple columns

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 14.2k 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.
  • P Offline
    P Offline
    panosk
    wrote on last edited by
    #2

    Up a little...

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

      Is that the "code":http://www.qtcentre.org/threads/12768-Need-to-have-columns-in-QComboBox?p=67105#post67105 you were thinking about ?

      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
      0
      • P Offline
        P Offline
        panosk
        wrote on last edited by
        #4

        [quote author="SGaist" date="1362473278"]Is that the "code":http://www.qtcentre.org/threads/12768-Need-to-have-columns-in-QComboBox?p=67105#post67105 you were thinking about ? [/quote]

        Thanks for you reply. I've read this and probably all other related snippets on the net, but it won't work in my case. I'm trying to display a QIcon and a QString (the first and second column in my model) in the combo box.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #5

          Hi!
          If i understand correctly you want something like this?:
          !http://s15.postimage.org/nfya32xuv/pscr.png?noCache=1362549902(printscr)!

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

            In that case, I would try to implement a proxy model that fetches the string for the text role and the icon for the decoration role. However I didn't test that so it's just an idea.

            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
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #7

              You already have custom model, just add decoration role case in your data method.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                panosk
                wrote on last edited by
                #8

                The data() method in my model is already implemented correctly. Actually, the very same model instance is used directly in a QTableView in the same QWizardPage where the QComboBox exists and it works fine. I think the problem lies on QComboBox's limitation to display only a single column (I suppose that's why there exists the setModelColumn() method, right?) for the current item although it allows multiple columns for the pop-up view.

                Here are two pics that show the situation:

                !http://s14.postimage.org/uhre7vxoh/combo_expanded.png(Combo expanded)!

                And the collapsed combo with some of the QWizardPage:

                !http://s14.postimage.org/4aq7bxfep/combo_collapsed.png(Combo collapsed)!

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qxoz
                  wrote on last edited by
                  #9

                  Here is some kind of pseudo code :)
                  @if(index.column() == columnWithText)
                  {
                  if (role == Qt::DecorationRole)
                  {
                  return ByteArrayToIcon(item.at(columnWitImage).toByteArray());
                  }
                  if (role == Qt::DisplayRole)
                  return item.at(columnWithText);
                  }@

                  and for combo box setModelColumn(columnWithText)
                  someting like that. For more correct example you can put your data code here.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    panosk
                    wrote on last edited by
                    #10

                    Thank you qxoz (and SGaist too, your idea was correct basically), your suggestion worked fine!
                    After I modified the data() method according to your suggestion, I just had to hide the first column from the views to avoid duplicating the QIcon. So basically, now the first column (QIcon) is bound to the second column (QString) for views, which is not a problem.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #11

                      I'm glad to hear it :)
                      Youre welcome.

                      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