When QDate in C++ is passes to Date in QML, converts to the previous day string in Qt 5.15.
Unsolved
QML and Qt Quick
-
When QDate in C++ passes to Date in QML, My program in Qt 5.15 converts to the previous day string.
I guess what occurs in Qt6.2 Document says.
- Qt 6.2 Document
If your program is run in a zone behind UTC (nominally west of The Prime Meridian), use of a date-only string will lead to a object whose is one less than the day-number in your string; i
But, Qt 5.15 document doesn't say it.
I have questions below.
- Is this also occur at Qt 5.15?
- I don't want to change QDate to QDatetime as possible. Can I do it?
Below my program code in parts.
- SampleClass.cpp
class SampleClass : public QObject { Q_OBJECT public: explicit SampleClass(); Q_INVOKABLE QDateTime getDatetime(); Q_INVOKABLE QDate getDate(); };
- main.cpp
int main(int argc, char *argv[]){ QGuiApplication app(argc,argv); qmlRegisterType<SampleClass>("sample",1,0,"Sample"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); :
- SampleClass.h
QDateTime SampleClass::getDatetime(){ QDateTime qdate_DEBUG = QDateTime(QDate(2022,9,10), QTime(0,0,0)); qInfo() << "[DEBUG] QDateTime:" << qdate_DEBUG.toString(); return qdate_DEBUG; } QDate SampleClass::getDate(){ QDate qdate_DEBUG = QDate(QDate(2022,9,10)); qInfo() << "[DEBUG] QDate:" << qdate_DEBUG.toString(); return qdate_DEBUG; }
- main.qml
Window{ : : Sample{id: sample} Component.onCompleted: { console.log("QDateTime: " +sample.getDatetime()) console.log("QDate : " +sample.getDate()) } : }
- Result
Only QDate in qml is conterted to the previous day.
[DEBUG] QDateTime: "Sat Sep 10 00:00:00 2022" qml: QDateTime: Sat Sep 10 00:00:00 2022 GMT-0700 [DEBUG] QDate: "Sat Sep 10 2022" qml: QDate : Fri Sep 9 17:00:00 2022 GMT-0700
- Qt 6.2 Document