Nominate our 2022 Qt Champions!

Failure to convert QDate to QDateTime

  • Hello!
    I have been trying to convert a QDate object to QDateTime.
    According to the QT specs it should be easy...
    When I debugged the code I could clearly see that "QDate temp" was instantiated correctly, however the conversion then fails.
    Why? I have passed a valid QDate to QDateTime's constructor...
    I have uploaded the screenshot of the debbuging window...
    Thank you very much!

        int year= this->day.toString("yyyy").toInt();
        int month= this->day.toString("MM").toInt();
        int day = this->day.toString("dd").toInt();
        QDate temp = QDate(year,month,day);
        QDateTime asd = QDateTime(temp);

  • Moderators

    Hi, welcome to devnet.

    It might be a silly question, but on which line do you have that breakpoint set? It looks like the asd variable is not yet initialized fully.
    On another note - not an error, but it's not usual to construct values through copy constructor syntax. Usually it's

    QDate temp(year,month,day);
    QDateTime asd(temp);

  • Hello!

    Thank you very much for stepping by!

    The breakpoint was set on the next line - so technically it should have been fully initialized.

    Maybe it's related to some user (OS) time format vs QT time format?
    I really can't think of anything.

  • Moderators

    The only other thing I can think of is that the variable is not used, so maybe something got optimized away? Is that a debug or a release build? What compiler are you using? What happens if you use the variable e.g. print it out (qDebug() << asd;)?

  • @Chris-Kawa

    Interesting development.
    When I output it to qDebug it actually prints the correct date. However in the debugger it's still saying Thu Jan 1 1970...

    My settings are:
    Kit: Qt 5.4.2 (msvc2012_opengl)
    Debugger: Auto-detected CDB (path) WindowsKits\8.0...

    So I guess the debugger is buggy and doesn't show the correct value, meanwhile it is indeed there in the object...

    Thank you very much for your help!
    Now im going to try comparing them see if indeed the values are stored.

  • Moderators

    It's also possible that you are debugging a release build. In that case things get optimized away and there will be dragons in the debugger. Click on the configuration button above the "play" button in Qt Creator and see which configuration are you running. There's Debug, Release and Profile. You should only run Debug builds through debugger.

  • Hello!

    Nope, it was always debugger.
    But it works. I just have to ignore that 1970 stuff in the debugger window.

    Thank you for the help :-)
    I guess I should use qDebug() more often.

Log in to reply