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; }
Option 2
while (startdate < enddate){ startdate.addDays(days); qDebug() << startdate; }
in QT ... I get an endless loop... Any ideas?
-
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?
@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; }
-
The function addDays() does not modify the object but returns a new one. See http://doc.qt.io/qt-5/qdatetime.html#addDays
-
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?
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(); }
-
Thanks all!
Very clear and concise.
Worked like a charm.
Much appreciated!I'll keep plugging along.
Thanks again.