Make the combo box drop down menus fit the text content even if the widget itself doesn't.

This commit is contained in:
Gaute Lindkvist 2019-11-25 08:11:40 +01:00
parent bf8ed71641
commit a7aef3f4e7
2 changed files with 25 additions and 5 deletions

View File

@ -230,6 +230,25 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName)
{
m_comboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
m_comboBox->setMinimumContentsLength(m_attributes.minimumContentsLength);
// Make sure the popup adjusts to the content even if the widget itself doesn't
QFont font = m_comboBox->view()->font();
int maxTextWidth = 0;
bool labelsElided = false;
for (const PdmOptionItemInfo& option : options)
{
QString label = option.optionUiText();
if (label.size() > m_attributes.maximumMenuContentsLength)
{
label.resize(m_attributes.maximumMenuContentsLength);
labelsElided = true;
}
maxTextWidth = std::max(maxTextWidth, QFontMetrics(font).boundingRect(label).width());
}
int marginWidth = m_comboBox->view()->contentsMargins().left() + m_comboBox->view()->contentsMargins().right();
m_comboBox->view()->setMinimumWidth(maxTextWidth + marginWidth);
m_comboBox->view()->setTextElideMode(labelsElided ? Qt::ElideMiddle : Qt::ElideNone);
}
if (m_attributes.enableEditableContent)

View File

@ -61,17 +61,18 @@ public:
adjustWidthToContents = false;
showPreviousAndNextButtons = false;
minimumContentsLength = 8;
maximumMenuContentsLength = 40;
enableEditableContent = false;
minimumWidth = -1;
iconSize = QSize(14, 14);
}
public:
bool adjustWidthToContents;
bool showPreviousAndNextButtons;
int minimumContentsLength; // The length of string to adjust to if adjustWidthToContents = false.
// Set to <= 0 to ignore and use AdjustToContentsOnFirstShow instead
bool adjustWidthToContents;
bool showPreviousAndNextButtons;
int minimumContentsLength; // The length of string to adjust to if adjustWidthToContents = false.
// Set to <= 0 to ignore and use AdjustToContentsOnFirstShow instead
int maximumMenuContentsLength;
bool enableEditableContent;
int minimumWidth;
QString placeholderText;