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. Increment a QDateTime -- but stop it from being an endless loop
Forum Updated to NodeBB v4.3 + New Features

Increment a QDateTime -- but stop it from being an endless loop

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 1.6k Views
  • 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.
  • S Offline
    S Offline
    ShylerC
    wrote on last edited by VRonin
    #1

    Team,

    I used to += -- but with ... startdate.addDays -- Super Confused! How to increment until condition is met?

    So in 'ol C++ regular I would do this -

    while (startdate < enddate) {
    startdate += days }
    
    //Would see my startdate increment until it met condition.
    

    Option 1.

    while (startdate < enddate){
                QDateTime NewTime;
                NewTime=startdate.addDays(days);
                qDebug() << NewTime;
            }
    

    Option 2

    while (startdate < enddate){
                startdate.addDays(days);
                qDebug() << startdate;
    }
    

    in QT ... I get an endless loop... Any ideas?

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • S ShylerC

      Team,

      I used to += -- but with ... startdate.addDays -- Super Confused! How to increment until condition is met?

      So in 'ol C++ regular I would do this -

      while (startdate < enddate) {
      startdate += days }
      
      //Would see my startdate increment until it met condition.
      

      Option 1.

      while (startdate < enddate){
                  QDateTime NewTime;
                  NewTime=startdate.addDays(days);
                  qDebug() << NewTime;
              }
      

      Option 2

      while (startdate < enddate){
                  startdate.addDays(days);
                  qDebug() << startdate;
      }
      

      in QT ... I get an endless loop... Any ideas?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ShylerC Signature of addDays is:

      QDateTime QDateTime::addDays(qint64 ndays) const
      

      That means it does NOT change the object on which it is called! It simply returns a NEW QDateTime object.
      Change your code to:

      while (startdate < enddate){
          startdate = startdate.addDays(days);
          qDebug() << startdate;
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The function addDays() does not modify the object but returns a new one. See http://doc.qt.io/qt-5/qdatetime.html#addDays

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        5
        • S ShylerC

          Team,

          I used to += -- but with ... startdate.addDays -- Super Confused! How to increment until condition is met?

          So in 'ol C++ regular I would do this -

          while (startdate < enddate) {
          startdate += days }
          
          //Would see my startdate increment until it met condition.
          

          Option 1.

          while (startdate < enddate){
                      QDateTime NewTime;
                      NewTime=startdate.addDays(days);
                      qDebug() << NewTime;
                  }
          

          Option 2

          while (startdate < enddate){
                      startdate.addDays(days);
                      qDebug() << startdate;
          }
          

          in QT ... I get an endless loop... Any ideas?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          hi
          @ShylerC said in Increment a QDateTime -- but stop it from being an endless loop:

          Team,

          I used to += -- but with ... startdate.addDays -- Super Confused! How to increment until condition is met?

          So in 'ol C++ regular I would do this -

          while (startdate < enddate) {
          startdate += days }

          //Would see my startdate increment until it met condition.

          Option 1.

          while (startdate < enddate){
                      QDateTime NewTime;
                      NewTime=startdate.addDays(days);
                      qDebug() << NewTime;
                  }
          

          in QT ... I get an endless loop... Any ideas?

          add Days returns a new QDateTime, but you don't do anything with it in your code example therefore startdate never changes.

           while (startdate < enddate){
                  startdate = startdate.addDays();
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • S Offline
            S Offline
            ShylerC
            wrote on last edited by
            #5

            Thanks all!

            Very clear and concise.
            Worked like a charm.
            Much appreciated!

            I'll keep plugging along.

            Thanks again.

            aha_1980A 1 Reply Last reply
            0
            • S ShylerC

              Thanks all!

              Very clear and concise.
              Worked like a charm.
              Much appreciated!

              I'll keep plugging along.

              Thanks again.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ShylerC: thanks for your feedback. then please mark this topic as SOLVED with the button Topic Tools.

              Thanks

              Qt has to stay free or it will die.

              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