QDate, QTime & QDateTime functions
-
What's wrong with QDate::addYears()?
auto newDate = QDateTime(curDateTime.date().addYear(1), curDateTime.time());
or
curDateTime.setDate(curDateTime.date().addYear(1)); -
@Christian-Ehrlicher
Thank you,
I'll try to play with it and see how it works .
It's still not as simple and bright as the option I suggested but if that's what it is, we'll get it right.
Thank you all. -
@Bracha
Yes it is true that it's a bit awkward to want to e.g. just set one element in an existingQDateTime
. It does not help that their are methods onQDate
&QTime
which help but not on a wholeQDateTime
. You just have to live with the way Qt does this.If it were me I would probably write a global "utility"
inline
function to do a particular operation you want on aQDateTime
, for the code you showed. -
you could, and should, make a constructor that excepts a QDateTime object as argument, that than can be done implicitly :D
-
@Christian-Ehrlicher I actually used your suggestion which is convenient for me at the moment but is probably a solution only for the date and not the time, Just want you to pay attention
-
Just want to summarize the proposed solutions in an orderly fashion so that it remains for the future:
@Christian-Ehrlicher: use QDate::addDays() / QDate::addMonths() / QDate::addYears(). [for dates]
example: [qDateTime.setDate(qDateTime.date().addYears(XXX)]@JonB : write a global "utility" inline function to do a particular operation you want on a QDateTime.
@J-Hilk: make a new class and inherit from QDateTime where you add those convenient setX functions.
don't forget to convert the returning objects from the existing functions that return a QDateTime object to the new object,
or make a constructor that excepts a QDateTime object as argument, that than can be done implicitly.Thank you all for a fruitful discussion and varied and helpful suggestions!