From f468f76455c91cf1c5b477ab9348337522b4d9aa Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Wed, 6 May 2020 15:41:56 +0200 Subject: Add an option to set the alignment of the text in QComboBox Change-Id: Ic9dca8864643302b2b3ff7dbd86260efdb0ab1d5 Reviewed-by: Volker Hilsheimer Reviewed-by: Richard Moe Gustavsen --- src/widgets/styles/qcommonstyle.cpp | 2 +- src/widgets/styles/qfusionstyle.cpp | 2 +- src/widgets/styles/qstyleoption.cpp | 9 ++++++++- src/widgets/styles/qstyleoption.h | 3 ++- src/widgets/styles/qstylesheetstyle.cpp | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src/widgets/styles') diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index a52e14eaf9..cc7af2ccc8 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -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(); diff --git a/src/widgets/styles/qfusionstyle.cpp b/src/widgets/styles/qfusionstyle.cpp index b9abedd626..dfd979cf7d 100644 --- a/src/widgets/styles/qfusionstyle.cpp +++ b/src/widgets/styles/qfusionstyle.cpp @@ -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); } diff --git a/src/widgets/styles/qstyleoption.cpp b/src/widgets/styles/qstyleoption.cpp index 122775879f..3194f5b941 100644 --- a/src/widgets/styles/qstyleoption.cpp +++ b/src/widgets/styles/qstyleoption.cpp @@ -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 diff --git a/src/widgets/styles/qstyleoption.h b/src/widgets/styles/qstyleoption.h index 52893ebd83..7f3fc4b49e 100644 --- a/src/widgets/styles/qstyleoption.h +++ b/src/widgets/styles/qstyleoption.h @@ -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; } diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 32f86e5b5a..13db17fbfb 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -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(); -- cgit v1.2.3