如何设置 QComboBox 宽度以适合最大的项目? [英] How do I set the QComboBox width to fit the largest item?
问题描述
我有一个 QComboBox
,我用 QString
填充:
I have a QComboBox
that I fill with QString
using:
comboBox->addItem(someString);
当我启动我的 GUI 应用程序时,QComboBox
的宽度总是 70,即使最小的项目要大得多.例如,如何将 QComboBox
的宽度动态设置为 comboBox
中最大的 QString
?
When I start my GUI application the width of the QComboBox
is always 70, even if the smallest item is much larger. How can I dynamically set the width of a QComboBox
, for instance, to the largest QString
within the comboBox
?
经过进一步测试后,我找到了以下解决方案:
After some further testing I found the following solution:
// get the minimum width that fits the largest item.
int width = ui->sieveSizeComboBox->minimumSizeHint().width();
// set the ComboBoxe to that width.
ui->sieveSizeComboBox->setMinimumWidth(width);
推荐答案
Qt (4.6) 在线文档对 QComboBox
有这样的说法:
Qt (4.6) online documentation has this to say about QComboBox
:
enum SizeAdjustPolicy { AdjustToContents, AdjustToContentsOnFirstShow, AdjustToMinimumContentsLength, AdjustToMinimumContentsLengthWithIcon }
我建议
确保
SizeAdjustPolicy
被实际使用
将枚举设置为 AdjustToContents
.正如您提到的 .ui
文件,我建议在 Designer 中执行此操作.通常,您的构造函数中根本不应该有任何与您在 Designer 中所做的事情有关的花哨的东西.
setting the enum to AdjustToContents
. As you mention a .ui
file I suggest doing that in Designer. Normally there shouldn't be anything fancy in your constructor at all concerning things you do in Designer.
这篇关于如何设置 QComboBox 宽度以适合最大的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!