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