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. How can I Increase the size of the Drop Down List of a QComboBox?
QtWS25 Last Chance

How can I Increase the size of the Drop Down List of a QComboBox?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 9 Posters 27.2k 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.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    Hi..

    I have a QComboBox, in that i add few items. (eg. item 1, item 2, combo box item 3, combo box item number 4, combo box item number is 5). Now i want to change the width size of the drop down list of the combo box that fits for the largest item string without changing the actual size of the QComboBox. How can i change the width size of the drop down list without changing the actual size of the QComboBox. Please give your suggestions.....

    @#include <QtGui>
    #include <QApplication>
    #include <QComboBox>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    QComboBox cb;
    cb.addItem("item 1");
    cb.addItem("item 2");
    cb.addItem("combo box item 3");
    cb.addItem("combo box item number 4");
    cb.addItem("combo box item number is 5");
    cb.show();
    return app.exec();
    }@

    Thanks & Regards

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      @
      comboBox->view()->setMinimumWidth(largestItemStringWidth)
      @
      You can calculate every string width with QFontMetrics.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on last edited by
        #3

        That works fine... :-) Thanks for your reply..

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

          Hi,

          Did you try to modify the "sizeAdjustPolicy":http://qt-project.org/doc/qt-4.8/qcombobox.html#sizeAdjustPolicy-prop ?

          Hope it helps

          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
          • J Offline
            J Offline
            jsveiga
            wrote on last edited by
            #5

            This seemed to be the default behavior on Qt4.8 (and maybe older?) (Windows).

            If you had a QComboBox with a fixed size and added items which were longer than it, when the popup listview opened it would automatically resize to the longest string content.

            I migrated apps from Qt4.8 to 5.0.2, and to my dismay, it does not work anymore.

            What should work then was to set the QComboBox view()->sizePolicy()->horizontalPolicy() to QSizePolicy::MinimumExpanding, but doing that simply adjusts the popup listview to whatever is set on the view's MinimumWidth. It does not expand!

            The default value for the horizontalPolicy() is already 13 (Ignore) which should expand too.

            I've tried both doing it before and after adding the items, and also tried reimplementing the QComboBox showPopup() to set that when the popup was opening (and tried both before and after calling QComboBox::showPopup() in the reimplemented method):

            @class ComboBox : public QComboBox
            {
            public:
            ComboBox(QWidget*);
            void showPopup();
            };

            ComboBox::ComboBox(QWidget * p) : QComboBox(p)
            {
            }

            void ComboBox::showPopup()
            {
            QComboBox::showPopup();
            QSizePolicy sp = view()->sizePolicy();
            sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
            view()->setMinimumWidth(100); //change this to see that it alone sets the width
            view()->setSizePolicy(sp);
            }
            @

            Now, is this a bug on Qt5 QComboBox (bugreports related to this are old and unresolved, although it worked on 4.8), or am I missing something?

            Apart from possibly reimplementing showPopup() to check the items one by one and setting the MinimumWidth "by hand" is there a way to get the 4.8 functionality back?

            tks
            Joao S Veiga

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tabacof
              wrote on last edited by
              #6

              I'm having exactly the same problem as Joao Veiga above. I've tried all the proposed solutions, searched the web for new ideas, and no matter what I do the list size does not change!

              Has this been reported as a bug? Is there a workaround?

              Thank you,
              Pedro Tabacof

              1 Reply Last reply
              0
              • I Offline
                I Offline
                ipanera
                wrote on last edited by
                #7

                That happens to me too.
                I have a defined a delegate, if i use it with a QListView it adjusts perfectly the size indicated on 'sizehint' but if I use the delegate on a 'QComboBox' the dropdown list does not match the size of the elements.
                Does anyone know anything more?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tapsa
                  wrote on last edited by
                  #8

                  Really? 2 years and not solved.
                  I am having this problem too and this result was first in Google search. Setting sizeAdjustPolicy changes both the drop down and the box itself, when only the drop down should be changed.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jalderete
                    wrote on last edited by
                    #9

                    Having the same issue here. Looks like it might be this ticket. https://bugreports.qt.io/browse/QTBUG-3097

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      reena jaus
                      wrote on last edited by
                      #10

                      this is the best solution

                      combobox->setStyleSheet("QComboBox QAbstractItemView::item {min-width: 60px; }");

                      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