Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QDateTime, setDate doesn't work properly
Forum Updated to NodeBB v4.3 + New Features

Problem with QDateTime, setDate doesn't work properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.9k 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.
  • narinder83N Offline
    narinder83N Offline
    narinder83
    wrote on last edited by
    #1

    QDateTime tempDate = date;
    tempDate.date().setDate(tempDate.date().year(), tempDate.date().month (), 1);
    tempDate.time ().setHMS (0,0,0);
    int d1 = tempDate.date ().day ();

    I'm expecting value as 1 but it is returning original value of day in date.

    raven-worxR Paul ColbyP 2 Replies Last reply
    0
    • narinder83N narinder83

      QDateTime tempDate = date;
      tempDate.date().setDate(tempDate.date().year(), tempDate.date().month (), 1);
      tempDate.time ().setHMS (0,0,0);
      int d1 = tempDate.date ().day ();

      I'm expecting value as 1 but it is returning original value of day in date.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @narinder83
      because tempDate.time() and tempDate.date() returns a copy time/data object.
      So altering these objects this way wont change anything on the tempDate object.

      Do this instead:

      QDateTime tempDate(date);
      tempDate.setDate( QDate(tempDate.date().year(), tempDate.date().month (), 1) );
      tempDate.setTime( QTime(0,0,0) );
      int d1 = tempDate.date ().day ();
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      5
      • narinder83N narinder83

        QDateTime tempDate = date;
        tempDate.date().setDate(tempDate.date().year(), tempDate.date().month (), 1);
        tempDate.time ().setHMS (0,0,0);
        int d1 = tempDate.date ().day ();

        I'm expecting value as 1 but it is returning original value of day in date.

        Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @narinder83

        The problem is here:

        tempDate.date().setDate(tempDate.date().year(), tempDate.date().month (), 1);
        //       ^^^^^
        

        The QDateTime::date() function is declared const - it returns a new date object that no longer relates to the tempDate it came from. So

        tempDate.date().setDate(tempDate.date().year(), tempDate.date().month (), 1);
        

        is identical to:

        QDate date = tempDate.date();
        date.setDate(tempDate.date().year(), tempDate.date().month (), 1);
        

        So you want to use QDateTime::setDate(), perhaps like:

        tempDate.setDate(QDate(tempDate.date().year(), tempDate.date().month (), 1));
        

        Cheers.

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          This:

          tempDate.setDate( QDate(tempDate.date().year(), tempDate.date().month (), 1) );
          

          can be simplified into

          tempDate.addDays( 1 - tempDate.date().day() );
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • m.sueM Offline
            m.sueM Offline
            m.sue
            wrote on last edited by
            #5

            said in Problem with QDateTime, setDate doesn't work properly:

            tempDate.date().setDate

            In this line happens the following: The function date() yields some result. This is temporary as you do not assign it to some variable. Then you change the temporary result with the setDate function. In the next line the temporary variable is lost together with your change.

            Later on you call the function date() again. It will which will return the same as in the first time, BUT of course without your change.

            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