How can I keep a QComboBox from changing width due to contents?
-
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.
-
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.
-
[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.
-
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()); } };