Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. When QDate in C++ is passes to Date in QML, converts to the previous day string in Qt 5.15.
Forum Updated to NodeBB v4.3 + New Features

When QDate in C++ is passes to Date in QML, converts to the previous day string in Qt 5.15.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 198 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m.kawakami
    wrote on last edited by m.kawakami
    #1

    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.

    • Qt 5.15 Document

    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
    
    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      Use QDateTime, not QDate.

      Date in QML also includes time.

      Edit: Notice this link does not mention using QDate at all. It is because the conversion is lossy and you will have date errors.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved