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



  • 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."





  • enum QComboBox::SizeAdjustPolicy

    This enum specifies how the size hint of the QComboBox should adjust when new content is added or content changes.

    Constant Value Description
    QComboBox::AdjustToContents 0 The combobox will always adjust to the contents
    QComboBox::AdjustToContentsOnFirstShow 1 The combobox will adjust to its contents the first time it is shown.
    QComboBox::AdjustToMinimumContentsLength 2 Use AdjustToContents or AdjustToContentsOnFirstShow instead.
    QComboBox::AdjustToMinimumContentsLengthWithIcon 3 The combobox will adjust to minimumContentsLength plus space for an icon. For performance reasons use this policy on large models.



  • Yes, I have read the SizeAdjustPolicy documentation. None of those say "do not adjust to contents."

    And, QSizePolicy is for use by the "owning" QLayout, right? I do want the QComboBox to get wider or narrower. I just don't want it to take matters into its own hands, which it seems to be doing.



  • Don't mix size policy (layout) and size adjust policy (combo box). Those are different settings. Size adjust policy should be what you are looking for.



  • 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?



  • 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.



  • 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.



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



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


  • Moderators

    [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.


  • Moderators

    [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.



  • 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;@



  • 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;@



  • @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.



  • 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()); }
    };
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.