Add an option to set the alignment of the text in QComboBox

Change-Id: Ic9dca8864643302b2b3ff7dbd86260efdb0ab1d5
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Andy Shaw 2020-05-06 15:41:56 +02:00
parent 6028e16b86
commit f468f76455
6 changed files with 18 additions and 6 deletions

View File

@ -2222,7 +2222,7 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
}
if (!cb->currentText.isEmpty() && !cb->editable) {
proxy()->drawItemText(p, editRect.adjusted(1, 0, -1, 0),
visualAlignment(cb->direction, Qt::AlignLeft | Qt::AlignVCenter),
visualAlignment(cb->direction, cb->textAlignment),
cb->palette, cb->state & State_Enabled, cb->currentText);
}
p->restore();

View File

@ -1059,7 +1059,7 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio
}
if (!cb->currentText.isEmpty() && !cb->editable) {
proxy()->drawItemText(painter, editRect.adjusted(1, 0, -1, 0),
visualAlignment(cb->direction, Qt::AlignLeft | Qt::AlignVCenter),
visualAlignment(cb->direction, cb->textAlignment),
cb->palette, cb->state & State_Enabled, cb->currentText,
cb->editable ? QPalette::Text : QPalette::ButtonText);
}

View File

@ -2611,7 +2611,7 @@ QStyleOptionComboBox::QStyleOptionComboBox(int version)
This enum is used to hold information about the version of the style option, and
is defined for each QStyleOption subclass.
\value Version 1
\value Version 2
The version is used by QStyleOption subclasses to implement
extensions without breaking compatibility. If you use
@ -2672,6 +2672,13 @@ QStyleOptionComboBox::QStyleOptionComboBox(int version)
\sa QStyle::SC_ComboBoxListBoxPopup
*/
/*!
\variable QStyleOptionGroupBox::textAlignment
\brief the alignment of the current text in the combo box
The default value is Qt::AlignLeft | Qt::AlignVCenter.
*/
/*!
\class QStyleOptionToolBox
\brief The QStyleOptionToolBox class is used to describe the

View File

@ -609,7 +609,7 @@ class Q_WIDGETS_EXPORT QStyleOptionComboBox : public QStyleOptionComplex
{
public:
enum StyleOptionType { Type = SO_ComboBox };
enum StyleOptionVersion { Version = 1 };
enum StyleOptionVersion { Version = 2 };
bool editable;
QRect popupRect;
@ -617,6 +617,7 @@ public:
QString currentText;
QIcon currentIcon;
QSize iconSize;
Qt::Alignment textAlignment = Qt::AlignLeft | Qt::AlignVCenter;
QStyleOptionComboBox();
QStyleOptionComboBox(const QStyleOptionComboBox &other) : QStyleOptionComplex(Version, Type) { *this = other; }

View File

@ -3894,7 +3894,7 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q
if (!cb->currentText.isEmpty() && !cb->editable) {
QPalette styledPalette(cb->palette);
rule.configurePalette(&styledPalette, QPalette::Text, QPalette::Base);
drawItemText(p, editRect.adjusted(0, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, styledPalette,
drawItemText(p, editRect.adjusted(0, 0, 0, 0), cb->textAlignment, styledPalette,
cb->state & State_Enabled, cb->currentText, QPalette::Text);
}
p->restore();

View File

@ -1240,8 +1240,12 @@ void QComboBox::initStyleOption(QStyleOptionComboBox *option) const
option->activeSubControls = d->hoverControl;
}
option->currentText = currentText();
if (d->currentIndex.isValid())
if (d->currentIndex.isValid()) {
option->currentIcon = d->itemIcon(d->currentIndex);
QVariant alignment = d->model->data(d->currentIndex, Qt::TextAlignmentRole);
if (alignment.isValid())
option->textAlignment = static_cast<Qt::Alignment>(alignment.toUInt());
}
option->iconSize = iconSize();
if (d->container && d->container->isVisible())
option->state |= QStyle::State_On;