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. Failure to convert QDate to QDateTime
Qt 6.11 is out! See what's new in the release blog

Failure to convert QDate to QDateTime

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 2 Posters 6.3k 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.
  • PhoenixP Offline
    PhoenixP Offline
    Phoenix
    wrote on last edited by
    #1

    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);
    

    http://postimg.org/image/oq7zc2pxr/

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);
      
      1 Reply Last reply
      0
      • PhoenixP Offline
        PhoenixP Offline
        Phoenix
        wrote on last edited by
        #3

        Hello!

        Thank you very much for stepping by!
        http://postimg.org/image/5m6e7a6jr/

        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.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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;)?

          PhoenixP 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            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;)?

            PhoenixP Offline
            PhoenixP Offline
            Phoenix
            wrote on last edited by Phoenix
            #5

            @Chris-Kawa

            Wow!
            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.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • PhoenixP Offline
                PhoenixP Offline
                Phoenix
                wrote on last edited by
                #7

                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.

                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