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.1k 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 1 Mar 2013, 15:47 last edited by
    #1

    I'm trying to display multiple columns on a QComboBox. It's almost finished, with one exception: the visible item on the combo box only displays one column. When expanded, everything looks good. I wouldn't like to use a delegate. Sad thing is that once I had a code snippet that could do that...

    Here is the relevant code for setting up the combo box:
    @
    QTableView *tblLan = new QTableView;

    ui->comboBox->setView(tblLan);
    

    //m_source is a custom model derived from QAbstractListModel with 3 columns
    ui->comboBox->setModel(m_source);
    //I only want to display the first 2 columns
    tblLan->setColumnHidden(2,true);
    //and the rest is code for making the tblLan appear like a list inside the combo box
    ...
    @

    Thanks in advance for any hints.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on 4 Mar 2013, 20:29 last edited by
      #2

      Up a little...

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 5 Mar 2013, 08:47 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 5 Mar 2013, 22:49 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 6 Mar 2013, 06:06 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 6 Mar 2013, 08:15 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 6 Mar 2013, 09:00 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 6 Mar 2013, 11:58 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 6 Mar 2013, 12:18 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 6 Mar 2013, 19:52 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 7 Mar 2013, 04:35 last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        1/11

                        1 Mar 2013, 15:47

                        • Login

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