How can I keep a QComboBox from changing width due to contents?
-
wrote on 6 Mar 2012, 00:02 last edited by
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."
-
wrote on 6 Mar 2012, 05:03 last edited by
"QWidget::sizePolicy":http://qt-project.org/doc/qt-4.8/qwidget.html#sizePolicy-prop
-
wrote on 6 Mar 2012, 08:31 last edited by
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. -
wrote on 6 Mar 2012, 17:16 last edited by
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.
-
wrote on 6 Mar 2012, 17:37 last edited by
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.
-
wrote on 7 Mar 2012, 17:59 last edited by
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?
-
wrote on 21 Nov 2014, 02:16 last edited by
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.
-
wrote on 21 Nov 2014, 02:16 last edited by
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.
-
wrote on 21 Nov 2014, 03:45 last edited by
Why don't you just truncate the striings of the combobox before adding them to the combobox
-
wrote on 21 Nov 2014, 03:45 last edited by
Why don't you just truncate the striings of the combobox before adding them to the combobox
-
[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.
-
[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.
-
wrote on 21 Nov 2014, 15:03 last edited by
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;@ -
wrote on 21 Nov 2014, 15:03 last edited by
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;@ -
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."
wrote on 18 Sept 2015, 10:41 last edited byI 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.
-
wrote on 9 Nov 2017, 12:07 last edited by
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()); } };
-
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()); } };
wrote on 12 Apr 2023, 07:56 last edited byAlso 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); } };