QDateTimeEdit: synchronize time-spec before initializing display
QDateTimeEdit ignores the time-spec of its date-time value, using its own time-spec instead; mostly, this works because it first conforms the value to its own time-spec. However, during construction, before doing this, it set up its display data, which could leave it with a different time (rather than a different representation of the given time) than it was asked to use. Moved the updateTimeSpec() calls to immediately after setting value in QDateTimeEditPrivate::init() to ensure correct handling. Added test. Task-number: QTBUG-54781 Change-Id: I3b07c10997abb858fc0b40558bff96e3fdabbd83 Reviewed-by: Jesus Fernandez <jesus.fernandez@qt.io> Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
a95d103bd2
commit
3379ace11b
@ -2388,18 +2388,21 @@ void QDateTimeEditPrivate::init(const QVariant &var)
|
|||||||
switch (var.type()) {
|
switch (var.type()) {
|
||||||
case QVariant::Date:
|
case QVariant::Date:
|
||||||
value = QDateTime(var.toDate(), QDATETIMEEDIT_TIME_MIN);
|
value = QDateTime(var.toDate(), QDATETIMEEDIT_TIME_MIN);
|
||||||
|
updateTimeSpec();
|
||||||
q->setDisplayFormat(defaultDateFormat);
|
q->setDisplayFormat(defaultDateFormat);
|
||||||
if (sectionNodes.isEmpty()) // ### safeguard for broken locale
|
if (sectionNodes.isEmpty()) // ### safeguard for broken locale
|
||||||
q->setDisplayFormat(QLatin1String("dd/MM/yyyy"));
|
q->setDisplayFormat(QLatin1String("dd/MM/yyyy"));
|
||||||
break;
|
break;
|
||||||
case QVariant::DateTime:
|
case QVariant::DateTime:
|
||||||
value = var;
|
value = var;
|
||||||
|
updateTimeSpec();
|
||||||
q->setDisplayFormat(defaultDateTimeFormat);
|
q->setDisplayFormat(defaultDateTimeFormat);
|
||||||
if (sectionNodes.isEmpty()) // ### safeguard for broken locale
|
if (sectionNodes.isEmpty()) // ### safeguard for broken locale
|
||||||
q->setDisplayFormat(QLatin1String("dd/MM/yyyy hh:mm:ss"));
|
q->setDisplayFormat(QLatin1String("dd/MM/yyyy hh:mm:ss"));
|
||||||
break;
|
break;
|
||||||
case QVariant::Time:
|
case QVariant::Time:
|
||||||
value = QDateTime(QDATETIMEEDIT_DATE_INITIAL, var.toTime());
|
value = QDateTime(QDATETIMEEDIT_DATE_INITIAL, var.toTime());
|
||||||
|
updateTimeSpec();
|
||||||
q->setDisplayFormat(defaultTimeFormat);
|
q->setDisplayFormat(defaultTimeFormat);
|
||||||
if (sectionNodes.isEmpty()) // ### safeguard for broken locale
|
if (sectionNodes.isEmpty()) // ### safeguard for broken locale
|
||||||
q->setDisplayFormat(QLatin1String("hh:mm:ss"));
|
q->setDisplayFormat(QLatin1String("hh:mm:ss"));
|
||||||
@ -2412,7 +2415,6 @@ void QDateTimeEditPrivate::init(const QVariant &var)
|
|||||||
if (QApplication::keypadNavigationEnabled())
|
if (QApplication::keypadNavigationEnabled())
|
||||||
q->setCalendarPopup(true);
|
q->setCalendarPopup(true);
|
||||||
#endif
|
#endif
|
||||||
updateTimeSpec();
|
|
||||||
q->setInputMethodHints(Qt::ImhPreferNumbers);
|
q->setInputMethodHints(Qt::ImhPreferNumbers);
|
||||||
setLayoutItemMargins(QStyle::SE_DateTimeEditLayoutItem);
|
setLayoutItemMargins(QStyle::SE_DateTimeEditLayoutItem);
|
||||||
}
|
}
|
||||||
|
@ -252,6 +252,7 @@ private slots:
|
|||||||
void timeSpec_data();
|
void timeSpec_data();
|
||||||
void timeSpec();
|
void timeSpec();
|
||||||
void timeSpecBug();
|
void timeSpecBug();
|
||||||
|
void timeSpecInit();
|
||||||
|
|
||||||
void monthEdgeCase();
|
void monthEdgeCase();
|
||||||
void setLocale();
|
void setLocale();
|
||||||
@ -3215,6 +3216,13 @@ void tst_QDateTimeEdit::timeSpecBug()
|
|||||||
QCOMPARE(oldText, testWidget->text());
|
QCOMPARE(oldText, testWidget->text());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QDateTimeEdit::timeSpecInit()
|
||||||
|
{
|
||||||
|
QDateTime utc(QDate(2000, 1, 1), QTime(12, 0, 0), Qt::UTC);
|
||||||
|
QDateTimeEdit widget(utc);
|
||||||
|
QCOMPARE(widget.dateTime(), utc);
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QDateTimeEdit::cachedDayTest()
|
void tst_QDateTimeEdit::cachedDayTest()
|
||||||
{
|
{
|
||||||
testWidget->setDisplayFormat("MM/dd");
|
testWidget->setDisplayFormat("MM/dd");
|
||||||
|
Loading…
Reference in New Issue
Block a user