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)); -
What's wrong with QDate::addYears()?
auto newDate = QDateTime(curDateTime.date().addYear(1), curDateTime.time());
or
curDateTime.setDate(curDateTime.date().addYear(1));wrote on 18 Jul 2022, 11:27 last edited by@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. -
@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.wrote on 18 Jul 2022, 11:56 last edited by@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. -
@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.@JonB or, hear me out,
you make a new class and inherit from QDateTime where you add those convenient setX functions....
-
@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. -
@JonB or, hear me out,
you make a new class and inherit from QDateTime where you add those convenient setX functions....
wrote on 18 Jul 2022, 12:35 last edited by@J-Hilk said in QDate, QTime & QDateTime functions:
you make a new class and inherit from QDateTime where you add those convenient setX functions....
But that does not work when existing functions return a
QDateTime
..... :( -
@J-Hilk said in QDate, QTime & QDateTime functions:
you make a new class and inherit from QDateTime where you add those convenient setX functions....
But that does not work when existing functions return a
QDateTime
..... :( -
you could, and should, make a constructor that excepts a QDateTime object as argument, that than can be done implicitly :D
-
What's wrong with QDate::addYears()?
auto newDate = QDateTime(curDateTime.date().addYear(1), curDateTime.time());
or
curDateTime.setDate(curDateTime.date().addYear(1));wrote on 18 Jul 2022, 14:26 last edited by@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
-
wrote on 18 Jul 2022, 14:28 last edited by Bracha
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!
14/14