QDate, QTime & QDateTime functions
-
Hi,
I am working on program with many function relate to date & time,
and I see that is no function to set units of date/time, like setMonth, setHour.
Every time I want to change a single item I need to build/update a temporary object of date/time and use setDate/SetHMS.
The code is much more cumbersome, and less readable.
As well as calls to the constructor and the creation of unnecessary objects that take up space in memory.
To me it is basic functions, and it is really requested that this be possible.
What can be done and acted upon for this?
Waiting to hear your opinion on the matter as well as guidance on action for creating these functions.
Thanks. -
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!
-
@Bracha said in QDate, QTime & QDateTime functions:
Every time I want to change a single item
What's your usecase for this? Creating a QDate/QTime is not that expensive.
-
In addition to @Christian-Ehrlicher: you can define macros to make your code easier to be read.
-
@Christian-Ehrlicher
Not necessarily expensive as well as cumbersome.
I have some conditions and I need change year/month/day accordingly, and i need to setDate with the parameters that already appear,
like: qDate_tmp.setDate(qDateTime_L.date().year() + 1, qDateTime_L.date().month(),
qDateTime_L.date().day());
qDateTime_L.setDate(qDate_tmp);
instead: qDateTime_L.setYear(qDateTime_L.date().year() + 1)@Axel-Spoerl
I don't like use Macro's -
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!