diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-05-15 12:35:08 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-05-28 06:14:32 +0000 |
commit | 5aaade8c935caa1add92f61187c0065e34cf7e0e (patch) | |
tree | ff25bd160f5a610e200816d99b1f01d3c2d0f225 /src/widgets/widgets/qdatetimeedit.cpp | |
parent | 11569f7071e5ac6b50672ad400e6ed6e70ecd76b (diff) |
Fix editing of QDateTimeEdit in 12-hour locales that don't use AM/PM
The code made two incorrect assumptions: that the strings used are "AM"
or "PM", or would be translated. Instead, the locale provides the
correct strings, and there is no need to translate. However, in order
not to break existing translations, we give those preference.
And that the AM/PM string is not longer than 4 characters, while in
e.g Spanish/Columbia locale the strings are "A. M." and "P. M.", ie 5
characters long. Also, the use of qMin in a function that is asked to
provide the maximum section length is wrong.
[ChangeLog][QWidgets][QDateTimeEdit] Use the information provided by
the locale to determine the AM/PM strings, unless they are already
translated.
Change-Id: I6d1b05376e5ac62fc58da2cdea2e6cb732ec6747
Fixes: QTBUG-72833
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/widgets/widgets/qdatetimeedit.cpp')
-rw-r--r-- | src/widgets/widgets/qdatetimeedit.cpp | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 3cebea77d6..3e6afdb586 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -2307,13 +2307,31 @@ void QDateTimeEdit::paintEvent(QPaintEvent *event) style()->drawComplexControl(QStyle::CC_ComboBox, &optCombo, &p, this); } +/* + Returns the string for AM and PM markers. + + If a translation for "AM" and "PM" is installed, then use that. + Otherwise, use the default implementation, which uses the locale. +*/ QString QDateTimeEditPrivate::getAmPmText(AmPm ap, Case cs) const { + QString original; + QString translated; if (ap == AmText) { - return (cs == UpperCase ? QDateTimeParser::tr("AM") : QDateTimeParser::tr("am")); + original = QLatin1String(cs == UpperCase ? "AM" : "am"); + translated = (cs == UpperCase ? QDateTimeParser::tr("AM") : QDateTimeParser::tr("am")); } else { - return (cs == UpperCase ? QDateTimeParser::tr("PM") : QDateTimeParser::tr("pm")); + original = QLatin1String(cs == UpperCase ? "PM" : "pm"); + translated = (cs == UpperCase ? QDateTimeParser::tr("PM") : QDateTimeParser::tr("pm")); } + + // This logic fails if a translation exists but doesn't change the string, + // which we can accept as a corner-case for which a locale-derived answer + // will be acceptable. + if (original != translated) + return translated; + + return QDateTimeParser::getAmPmText(ap, cs); } int QDateTimeEditPrivate::absoluteIndex(QDateTimeEdit::Section s, int index) const |