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 keep a QComboBox from changing width due to contents?
Forum Updated to NodeBB v4.3 + New Features

How can I keep a QComboBox from changing width due to contents?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 10 Posters 25.3k 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.
  • D Offline
    D Offline
    ddunham
    wrote on last edited by
    #6

    I admit I haven't stepped into Qt yet, but it seems like it's QComboBox that's confusing them. The size policy is working great until I happen to populate the QComboBox with something long, and then it makes itself too wide.

    So what size adjust policy should I set, to have the QComboBox never make up a size hint that's too big?

    1 Reply Last reply
    1
    • Z Offline
      Z Offline
      zweistein
      wrote on last edited by
      #7

      Can someone please answer the question?

      There is no option to use a fixed size for QComboBox, because QComboBox keeps resizing its size either "on first show = AdjustToContentsOnFirstShow" or "always = AdjustToContents" but never "never". Just as the topic starter described.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zweistein
        wrote on last edited by
        #8

        Can someone please answer the question?

        There is no option to use a fixed size for QComboBox, because QComboBox keeps resizing its size either "on first show = AdjustToContentsOnFirstShow" or "always = AdjustToContents" but never "never". Just as the topic starter described.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DBoosalis
          wrote on last edited by
          #9

          Why don't you just truncate the striings of the combobox before adding them to the combobox

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DBoosalis
            wrote on last edited by
            #10

            Why don't you just truncate the striings of the combobox before adding them to the combobox

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #11

              [quote author="zweistein" date="1416536210"]Can someone please answer the question?

              There is no option to use a fixed size for QComboBox, because QComboBox keeps resizing its size either "on first show = AdjustToContentsOnFirstShow" or "always = AdjustToContents" but never "never". Just as the topic starter described.[/quote]There's an option available to all QWidgets: Set the minimum and maximum width to the same value.

              Alternatively, you could leave the policy at AdjustToContentsOnFirstShow, and then resize the combo box after it's shown.

              [quote author="DBoosalis" date="1416541500"]Why don't you just truncate the striings of the combobox before adding them to the combobox
              [/quote]It's not trivial to calculate the number of characters to truncate though.

              Anyway, if you keep the policy at "AdjustToContentsOnFirstShow" or if you force it to have a fixed size, the QComboBox will automatically elide (truncate) your added text if necessary.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #12

                [quote author="zweistein" date="1416536210"]Can someone please answer the question?

                There is no option to use a fixed size for QComboBox, because QComboBox keeps resizing its size either "on first show = AdjustToContentsOnFirstShow" or "always = AdjustToContents" but never "never". Just as the topic starter described.[/quote]There's an option available to all QWidgets: Set the minimum and maximum width to the same value.

                Alternatively, you could leave the policy at AdjustToContentsOnFirstShow, and then resize the combo box after it's shown.

                [quote author="DBoosalis" date="1416541500"]Why don't you just truncate the striings of the combobox before adding them to the combobox
                [/quote]It's not trivial to calculate the number of characters to truncate though.

                Anyway, if you keep the policy at "AdjustToContentsOnFirstShow" or if you force it to have a fixed size, the QComboBox will automatically elide (truncate) your added text if necessary.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DBoosalis
                  wrote on last edited by
                  #13

                  How about doing something like this to figure it out:

                  @QFont fnt = comboBox->font()
                  QFontMetrics fm(fnt);
                  int charWidth = fm.width(QChar('a''));
                  int numOfCharactors = myMaxWdith %charWidth;@

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DBoosalis
                    wrote on last edited by
                    #14

                    How about doing something like this to figure it out:

                    @QFont fnt = comboBox->font()
                    QFontMetrics fm(fnt);
                    int charWidth = fm.width(QChar('a''));
                    int numOfCharactors = myMaxWdith %charWidth;@

                    1 Reply Last reply
                    0
                    • D ddunham

                      I've got a layout which can be variable width -- a QComboBox can be wider if its container is wide.

                      Unfortunately, with some contents in the menu, the QComboBox ends up wider than its parent.

                      QComboBox::SizeAdjustPolicy seems to indicate when to measure & adjust, but I don't see a way to say "don't adjust at all, let my parent control me, it's OK to truncate menu items."

                      G Offline
                      G Offline
                      galinette
                      wrote on last edited by
                      #15

                      @ddunham

                      I have actually found a solution. If you set the QComboBox minimumContentsLength property to some small value greater than 0, and the sizeAdjustPolicy to AdjustToContents, the combo box will try to adjust its size, but will fit inside the layout requirements.

                      If minimumContentsLength is 0, it will enforce a fixed size to fit all its contents, and the layout will never size it down, as you have noticed.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kfrosty
                        wrote on last edited by
                        #16

                        A bit late but ... overwriting sizeHint() prevents from adjusting to contents at all.

                        class NoAdjustComboBox : public QComboBox
                        {
                        public:
                          QSize sizeHint() const override        { return minimumSizeHint(); }
                          QSize minimumSizeHint() const override { return QSize(50, QComboBox::minimumSizeHint().height()); }
                        };
                        
                        L 1 Reply Last reply
                        0
                        • K kfrosty

                          A bit late but ... overwriting sizeHint() prevents from adjusting to contents at all.

                          class NoAdjustComboBox : public QComboBox
                          {
                          public:
                            QSize sizeHint() const override        { return minimumSizeHint(); }
                            QSize minimumSizeHint() const override { return QSize(50, QComboBox::minimumSizeHint().height()); }
                          };
                          
                          L Offline
                          L Offline
                          loger9
                          wrote on last edited by
                          #17

                          Also much more late but I fixed this by simply overriding showEvent to not do anything.

                          class NoAdjustComboBox : public QComboBox
                          {
                          public:
                            void showEvent(QShowEvent* e)  override {
                          	return QWidget::showEvent(e);
                            }
                          };
                          
                          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